











语法:
array.filter(function(currentValue,index,arr), thisValue)
currentValue:必须。当前元素的值index:可选。当前元素的索引值(下标)arr:可选。当前元素属于的数组对象thisValue:可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。如果省略了 thisValue ,"this" 的值为 "undefined"。语法:
array.map(function(currentValue,index,arr), thisValue)
currentValue:必须。当前元素的值index:可选。当前元素的索引值arr:可选。当前元素属于的数组对象thisValue:可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。如果省略了 thisValue,或者传入 null、undefined,那么回调函数的 this 为全局对象。语法:
array.forEach(function(currentValue, index, arr), thisValue)
currentValue:必须。当前元素index:可选。当前元素的索引值。arr:可选。当前元素所属的数组对象。thisValue:可选。传递给函数的值一般用 "this" 值。如果这个参数为空, "undefined" 会传递给 "this" 值。export default {
name: 'Array',
components: {},
data() {
return {
array: [
{ label: '数组1', value: 234 },
{ label: '数组2', value: 854 },
{ label: '数组3', value: 12 },
{ label: '数组4', value: 410 }
],
target: []
}
},
computed: {},
created() {},
methods: {
filter() {
this.target = this.array.filter((item, index, arr) => {
return item.value > 400
}, this)
console.log(this.target)
语法:
array.find(function(currentValue, index, arr),thisValue)
currentValue:必需。当前元素index:可选。当前元素的索引值arr:可选。当前元素所属的数组对象thisValue:可选。 传递给函数的值一般用 "this" 值。如果这个参数为空, "undefined" 会传递给 "this" 值。语法:
array.findIndex(function(currentValue, index, arr), thisValue)
currentValue:必需。当前元素index:可选。当前元素的索引值arr:可选。当前元素所属的数组对象thisValue:可选。 传递给函数的值一般用 "this" 值。如果这个参数为空, "undefined" 会传递给 "this" 值。find() {
this.target = this.array.find((item, index, arr) => {
return item.value > 400
})
console.log(this.target)
push() {
console.log(this.array.push(123, 43))
语法:
array.splice(index,howmany,item1,.....,itemX)
index:必需。规定从何处添加/删除元素。该参数是开始插入和(或)删除的数组元素的下标,必须是数字。howmany:可选。规定应该删除多少元素。必须是数字,但可以是 "0"。如果未规定此参数,则删除从 index 开始到原数组结尾的所有元素。item1,.....,itemX:可选。要添加到数组的新元素语法:
array.slice(start, end)
start:可选 。
end:可选。
splice() {
this.target = this.array.splice(1, 2)
语法:
array.indexOf(item,start)
item:必须。查找的元素。start:可选的整数参数。
语法:
array.lastIndexOf(item,start)
item:必须。规定需检索的字符串值。start:可选的整数参数。
// arrays: ['sss', 'err', 'sfs', 'fghdj', 'sfs', 'dshx'],
indexOf() {
this.target = this.arrays.indexOf('sfs')
语法:
array.some(function(currentValue,index,arr),thisValue)
currentValue:必须。当前元素的值index:可选。当前元素的索引值arr:可选。当前元素属于的数组对象thisValue:可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。如果省略了 thisValue ,"this" 的值为 "undefined"。语法:
array.every(function(currentValue,index,arr), thisValue)
currentValue:必须。当前元素的值。index:可选。当前元素的索引值。arr:可选。当前元素属于的数组对象。thisValue:可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。如果省略了 thisValue ,"this" 的值为 "undefined"。// arrays: ['sss', 'err', 'sfs', 'fghdj', 'sfs', 'dshx'],
some() {
this.target = this.arrays.some((item, index, arr) => item === 'sfs')
| filter | map | forEach |
|---|---|---|
| 不改变原数组 | 不改变原数组 | 改变原数组 |
| 返回新数组 | 返回新数组 | 不返回新数组 |
| 不对空数组进行检测 | 不对空数组进行检测 | 不对空数组进行检测 |
| find | findIndex |
|---|---|
| 返回第一个元素的值 | 返回第一个元素位置 |
| 在测试条件时返回 true 时,之后的值不会再调用 | 在测试条件时返回 true 时,之后的值不会再调用 |
| 对于空数组,函数不会执行 | 对于空数组,函数不会执行 |
| 没有改变数组的原始值 | 没有改变数组的原始值 |
| push | pop | shfit | unshift |
|---|---|---|---|
array.push(item1, item2, ..., itemX) |
array.pop() |
array.shift() |
array.unshift(item1,item2, ..., itemX) |
| 数组末尾添加一个或多个元素 | 删除数组最后一个元素 | 删除数组的第一个元素 | 数组的开头添加一个或更多元素 |
| 返回新的长度 | 返回删除的元素 | 返回第一个元素的值 | 返回新的长度 |
| 改变数组的长度 | 改变数组的长度 | 改变数组的长度 | 改变数组的数目 |
| splice | slice |
|---|---|
| 添加或删除数组中的元素 | 从已有的数组中返回选定的元素(截取) |
| 会改变原始数组 | 不会改变原始数组 |
| 返回删除元素形成的数组 | 返回选定的元素 |
| indexOf | lastIndexOf |
|---|---|
| 返回数组中某个指定的元素位置 | 返回一个指定的元素在数组中最后出现的位置 |
| 从头到尾地检索数组 | 从尾到头地检索数组 |
| 返回 item 的第一次出现的位置 | 返回 item 从尾向前检索第一个次出现在数组的位置 |
| some | every |
|---|---|
| 用于是否满足指定条件 | 用于所有元素是否都符合指定条件 |
| 有一个元素满足条件,则表达式返回true | 所有元素都满足条件,则返回 true |
| 不会改变原始数组 | 不会改变原始数组 |
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。