for example
the let ARR = [ . 1 , 2 , . 3 , . 4 ]
the let Arr = [
{ ID: 0, name: 'Bob' } ,
{ ID:. 1, name: 'white' } ,
{ ID: 2, name: 'Small red ' } ,
{ ID:. 3, name:' new small ' }
]
for
for (let i = 0; i < 6; i++) {
}
for in
Through the array index
for (i in arr) {
console.log(i);
}
// 0、1、2、3
for of
Through the array element value
for (v in arr) {
console.log(i);
}
// 1、2、3、4
forEach
Through each item in the array, no return value, no effect on the original array
arr.forEach ((value, index, arr) => {
} );
value required, the current element
index Alternatively, the current value of the index element
arr Alternatively, the object of the current array element belongs
indexOf
Returns the location of a specified value for the first time in the array
arr.indexOf (value, fromIndex)
value required, a predetermined value of the string to be retrieved.
fromindex optional integer parameter specified in the retrieved start position of the string
lastIndexOf
Returns the location of a specified value in the array of the last occurrence, use the same indexOf
find
Return to a qualified first element of the array of values
Common Syntax: arr.find (value, index, ARR) => {} )
the let A = Arr.findIndex (V => === V.ID 2 )
the console.log (A); // {ID: 2, name: 'red', age: 3}
findIndex
Return to a qualified first element of the array position
常用语法: arr.findIndex(value,index,arr) => {})
let a = Arr.findIndex(v => v.id === 2) console.log(a); //2
includes
判断数组是否包含指定的元素值
arr.includes(value, fromIndex)
value 必须,需要查找的元素值。
fromIndex 可选,从该索引处开始查找
every
检测数组所有元素是否都符合指定条件,如果数组中检测到有一个元素不满足,则整个表达式返回 false
常用语法: arr.every(value,index,arr) => {})
let a = Arr.findIndex(v => v.id > 2)
console.log(a); //false
some
检测数组所有元素是否都符合指定条件,如果有一个元素满足条件,则表达式返回true
常用语法: arr.some((value,index,arr) => {})
let a = Arr.findIndex(v => v.id > 2)
console.log(a); //true
map
返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值,不影响原数组
常用语法: arr.map(value,index,arr) => {})
let a = Arr.map( (v,i) => {
return v.id * i
})
console.log(a); //[ 0, 1, 4, 9 ]
filter
返回一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素,不影响原数组
常用语法: arr.filter(value,index,arr) => {})
let a = Arr.filter((v,i) => {
return v.id > 1
})
console.log(a); //[ { id: 2, name: '小红', age: 3 } ]
reduce
接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值
常用语法: arr.reduce((total, value, index, arr) => {})
total: 必需,初始值, 或者计算结束后的返回值
let a = arr.reduce((total,v) => {
return total + v
})
console.log(a); //10
reduceRight
和 reduce 功能是一样的,不同的是 reduceRight() 从数组的末尾向前将数组中的数组项做累加
keys、values、entries
返回一个数组的迭代对象,可以用 for of 进行遍历
keys遍历原数组键,values遍历原数组值,entries遍历原数组键值对