内容导读
数组迭代器的九个方法
- ①:
array.forEach() - ②:
array.map() - ③:
array.filter() - ④:
array.find() - ⑤:
array.findIndex() - ⑥:
array.every() - ⑦:
array.some() - ⑧:
array.reduce() - ⑨:
array.reduceRight()
迭代器方法
迭代器方法用于遍历数组元素,对每个数组元素调用一次指定的函数。
①:forEach()
定义:
forEach()方法按顺序为数组中的每个元素调用一次函数。
语法:
array.forEach(function(currentValue, index, arr), thisValue)
参数:
function(currentValue, index, arr):必需。为数组中的每个元素运行的函数。函数参数:
currentValue:必需。当前元素的值。
index:可选。当前元素的数组索引。
arr:可选。当前元素所属的数组对象
thisValue:可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,则值 "undefined" 将作为其 "this" 值传递。
可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,则值 "undefined" 将作为其 "this" 值传递。
返回值:
- undefined
注意 :
-
forEach()不存在break机制。
示例:
- 对于数组中的每个元素:将值更新为原始值的 10 倍
let numbers = [65, 44, 12, 4];
numbers.forEach(myFunction)
function myFunction(item, index, arr) {
arr[index] = item * 10
}
②:map()
定义:
-
map()方法使用为每个数组元素调用函数的结果创建新数组。
语法:
array.map(function(currentValue, index, arr), thisValue)
参数:
-
function(currentValue, index, arr):必需。为数组中的每个元素运行的函数。 - 函数参数:
currentValue:必需。当前元素的值。
index:可选。当前元素的数组索引。
arr:可选。当前元素所属的数组对象
thisValue:可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,则值 "undefined" 将作为其 "this" 值传递。
返回值:
- 数组,包含为原始数组中的每个元素调用提供的函数的结果。
示例:
- 将数组中的所有值乘以 10
let numbers = [65, 44, 12, 4]
let newarray = numbers.map(myFunction)
function myFunction(num) {
return num * 10
}
document.getElementById("demo").innerHTML = newarray
③:filter()
定义:
-
filter()方法创建数组,其中填充了所有通过测试的数组元素(作为函数提供)。
语法:
array.filter(function(currentValue, index, arr), thisValue)
参数:
function(currentValue, index, arr):必需。为数组中的每个元素运行的函数。函数参数:
currentValue:必需。当前元素的值。
index:可选。当前元素的数组索引。
arr:可选。当前元素所属的数组对象
thisValue:可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,则值 "undefined" 将作为其 "this" 值传递。
可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,则值 "undefined" 将作为其 "this" 值传递。
返回值:
- 包含所有通过测试的数组元素的数组。如果没有元素通过测试,则返回一个空数组。
示例:
- 输出[4,5,6]
let arr = [1, 2, 3]
let arrNew = [ ]
arrNew = arr.map(function(item,index){
return item + 3 // 设置function函数的返回值,返给map()方法
});
console.log(arrNew) // [4,5,6]
④:find()
定义:
-
find()方法对数组中存在的每个元素执行一次函数:如果找到函数返回true值的数组元素,则find()返回该数组元素的值(并且不检查剩余值),否则返回undefined
语法:
array.find(function(currentValue, index, arr), thisValue)
参数:
function(currentValue, index, arr):必需。为数组中的每个元素运行的函数。函数参数:
currentValue:必需。当前元素的值。
index:可选。当前元素的数组索引。
arr:可选。当前元素所属的数组对象
thisValue:可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,则值 "undefined" 将作为其 "this" 值传递。
可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,则值 "undefined" 将作为其 "this" 值传递。
返回值:
- 如果数组中的任何元素通过测试,则返回数组元素值,否则返回 undefined。
示例:
let arr = [1,2,3]
let result = arr.find(function(item){
return item == 3
})
console.log(result) // 3
⑤:findIndex()
定义:
findIndex() 方法对数组中存在的每个元素执行一次函数:如果找到函数返回 true 值的数组元素,则 findIndex() 返回该数组元素的索引(并且不检查剩余值),否则返回 -1
语法:
array.findIndex(function(currentValue, index, arr), thisValue)
参数:
function(currentValue, index, arr):必需。为数组中的每个元素运行的函数。函数参数:
currentValue:必需。当前元素的值。
index:可选。当前元素的数组索引。
arr:可选。当前元素所属的数组对象
thisValue:可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,则值 "undefined" 将作为其 "this" 值传递。
可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,则值 "undefined" 将作为其 "this" 值传递。
返回值:
- 如果数组中的任何元素通过测试,则返回数组元素索引,否则返回 -1。
示例:
let arr = [1,2,3,4,5]
let sum = arr.findIndex(function(item){
return item > 5
})
console.log(arr,sum)//[ 1, 2, 3, 4, 5 ] -1
⑥:every()
定义:
-
every()方法对数组中存在的每个元素执行一次函数:如果找到函数返回 false 值的数组元素,every()返回false(并且不检查剩余值),如果没有出现false,every()返回true
语法:
array.every(function(currentValue, index, arr), thisValue)
参数:
function(currentValue, index, arr):必需。为数组中的每个元素运行的函数。函数参数:
currentValue:必需。当前元素的值。
index:可选。当前元素的数组索引。
arr:可选。当前元素所属的数组对象
thisValue:可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,则值 "undefined" 将作为其 "this" 值传递。
可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,则值 "undefined" 将作为其 "this" 值传递。
返回值:
- 布尔值。如果数组中的所有元素都通过测试,则返回 true,否则返回 false.
示例:
let arr = [1,2,3,4,5]
arr.every(function(tiem){
console.log(tiem > 0)//true
})
⑦:some()
定义:
-
some()方法对数组中存在的每个元素执行一次函数:如果找到函数返回真值的数组元素,some() 返回真(并且不检查剩余值),否则返回 false
语法:
array.some(function(currentValue, index, arr), thisValue)
参数:
function(currentValue, index, arr):必需。为数组中的每个元素运行的函数。函数参数:
currentValue:必需。当前元素的值。
index:可选。当前元素的数组索引。
arr:可选。当前元素所属的数组对象
thisValue:可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,则值 "undefined" 将作为其 "this" 值传递。
可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,则值 "undefined" 将作为其 "this" 值传递。
返回值:
- 布尔值。如果数组中的任何元素通过测试,则返回 true,否则返回 false。
示例:
let arr = [1,2,3,4,5]
let s = arr.some(function(item){
return item > 5
})
console.log(arr,s)//[ 1, 2, 3, 4, 5 ] false
⑧:reduce()
定义:
-
reduce()方法为数组的每个值(从左到右)执行提供的函数。
函数的返回值存储在累加器中(结果/总计)。
语法:
array.reduce(function(currentValue, index, arr), thisValue)
参数:
function(currentValue, index, arr):必需。为数组中的每个元素运行的函数。函数参数:
currentValue:必需。当前元素的值。
index:可选。当前元素的数组索引。
arr:可选。当前元素所属的数组对象
thisValue:可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,则值 "undefined" 将作为其 "this" 值传递。
可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,则值 "undefined" 将作为其 "this" 值传递。
返回值:
- 返回上次调用回调函数的累积结果。
示例:
let arr = [1,2,3,4,5]
let sum = arr.reduce(function(c,item,index,arr){
return c + item
})
console.log(arr,sum)// [1,2,3,4,5] 15
⑨:reduceRight()
定义:
-reduceRight() 方法为数组的每个值(从右到左)执行提供的函数。
函数的返回值存储在累加器中(结果/总计)。
语法:
array.reduceRight(function(total, currentValue, currentIndex, arr), initialValue)
参数:
-
function(total, currentValue, index, arr):必需。为数组中的每个元素运行的函数。 - 函数参数:
total:必需。initialValue,或函数先前返回的值。
currentValue:必需。当前元素的值。
index:可选。当前元素的数组索引。
arr:可选。当前元素所属的数组对象
initialValue:可选。作为初始值传递给函数的值。
返回值:
- 返回上次调用回调函数的累积结果。
示例:
let arr = [1,2,3,4,5]
let s = arr.reduceRight(function(c,item,index,arr){
return c - item
})
console.log(arr,s)//[ 1, 2, 3, 4, 5 ]-5
ps:拜拜~