JavaScript 数组遍历方法

拾人牙慧,感谢先行者们!

ES5:
forEach、every 、some、 fliter、map、reduce、reduceRight
ES6:
find、findIndex、keys、values、entries

ES5

forEach

forEach
语法:
array.forEach(function(currentValue, index, arr), thisValue)

参数:
function(必须): 数组中每个元素需要调用的函数。

// 回调函数的参数
currentValue(必须),数组当前元素的值。
index(可选), 当前元素的索引值。
arr(可选),数组对象本身。

thisValue(可选): 当执行回调函数时this绑定对象的值,默认值为undefined。

知识点:
无法中途退出循环,只能用return退出本次回调,进行下一次回调。
它总是返回 undefined,即使你return了一个值。

let arr = [1, 2, 3];
let result = arr.foreEach(function(item, index, arr){
  console.log(item) // 1 2 3
  return item
});
// result  返回值 undefined

every

every 检测数组所有元素是否都符合判断条件
定义: 方法用于检测数组所有元素是否都符合函数定义的条件

语法:
array.every(function(currentValue, index, arr), thisValue)

参数:
function(必须): 数组中每个元素需要调用的函数。

// 回调函数的参数
currentValue(必须),数组当前元素的值
index(可选), 当前元素的索引值
arr(可选),数组对象本身

thisValue(可选): 当执行回调函数时this绑定对象的值,默认值为undefined

知识点:
如果数组中检测到有一个元素不满足,则整个表达式返回 false,且剩余的元素不会再进行检测。
如果所有元素都满足条件,则返回 truelet result = [1, 2, 3].every(function(item){
  return item > 0
});
// result  true 数组元素 123都大于0 返回 true

some

some 数组中的是否有满足判断条件的元素
语法:
array.some(function(currentValue, index, arr), thisValue)

参数:
function(必须): 数组中每个元素需要调用的函数。

// 回调函数的参数
currentValue(必须),数组当前元素的值
index(可选), 当前元素的索引值
arr(可选),数组对象本身

thisValue(可选): 当执行回调函数时this绑定对象的值,默认值为undefined

知识点:
如果有一个元素满足条件,则表达式返回true, 剩余的元素不会再执行检测。
如果没有满足条件的元素,则返回falselet result = [1, 2, 10].some(function(item){
  return item > 3
});
// result  true 数组元素 10大于3 返回 true

filter

filter 过滤原始数组,返回新数组
语法:
let new_array = arr.filter(function(currentValue, index, arr), thisValue)

参数:
function(必须): 数组中每个元素需要调用的函数。

/// 回调函数的参数
currentValue(必须),数组当前元素的值
index(可选), 当前元素的索引值
arr(可选),数组对象本身

thisValue(可选): 当执行回调函数时this绑定对象的值,默认值为undefined

知识点:
返回满足条件的元素数组

let result = [1, 2, 3].filter(function(item){
  return item > 2
});
// result [3]

map

map 对数组中的每个元素进行处理,返回新的数组
语法:
let new_array = arr.map(function(currentValue, index, arr), thisValue)

参数:
function(必须): 数组中每个元素需要调用的函数。

// 回调函数的参数
currentValue(必须),数组当前元素的值
index(可选), 当前元素的索引值
arr(可选),数组对象本身

thisValue(可选): 当执行回调函数时this绑定对象的值,默认值为undefined

知识点
返回一个新数组,可以对每个元素进行操作,再返回

let result = [1, 2, 3].map(function(item){
  return item +1
});
// result [2, 3, 4] 对每项+1
let objArr = [
  {
    name: '张三',
    age: 18
  },
  {
    name: '李四',
    age: 18
  },
  {
    name: '王五',
    age: 18
  },
]
let result2 = objArr.map(function(item){
  return item.name
});
// result [ '张三', '李四', '王五']   返回 所有姓名。一个新的数组

reduce

reduce 为数组提供累加器,合并为一个值
语法:
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

参数:
function(必须): 数组中每个元素需要调用的函数。

// 回调函数的参数
total(必须),初始值, 或者上一次调用回调返回的值
currentValue(必须),数组当前元素的值
index(可选), 当前元素的索引值
arr(可选),数组对象本身

initialValue(可选): 指定第一次回调 的第一个参数。

/**
回调第一次执行时:
如果 initialValue 在调用 reduce 时被提供,那么第一个 total 将等于 initialValue,此时 currentValue 等于数组中的第一个值;
如果 initialValue 未被提供,那么 total 等于数组中的第一个值,currentValue 等于数组中的第二个值。此时如果数组为空,那么将抛出 TypeError。
如果数组仅有一个元素,并且没有提供 initialValue,或提供了 initialValue 但数组为空,那么回调不会被执行,数组的唯一值将被返回。
*/
let result = [0, 1, 2, 3].reduce(function (a, b) {
 return a + b;
}, 0);
// result 6

reduceRight 从右至左累加
这个方法除了与reduce执行方向相反外,其他完全与其一致,请参考上述 reduce 方法介绍。

ES6

find & findIndex

find() & findIndex() 根据条件找到数组成员。
find() 定义:用于找出第一个符合条件的数组成员,并返回该成员,如果没有符合条件的成员,则返回undefinedfindIndex() 定义:返回第一个符合条件的数组成员的位置,如果所有成员都不符合条件,则返回-1。

语法:
let new_array = arr.find(function(currentValue, index, arr), thisValue)
let new_array = arr.findIndex(function(currentValue, index, arr), thisValue)

参数:
function(必须): 数组中每个元素需要调用的函数。

// 回调函数的参数
currentValue(必须),数组当前元素的值
index(可选), 当前元素的索引值
arr(可选),数组对象本身

thisValue(可选): 当执行回调函数时this绑定对象的值,默认值为undefined
这两个方法都可以识别NaN,弥补了indexOf的不足.

// find
let result1 = [1, 4, -5, 10].find((n) => n < 0); // 返回元素-5
let result2  = [1, 4, -5, 10,NaN].find((n) => Object.is(NaN, n)); // 返回元素NaN
// findIndex
let result3 = [1, 4, -5, 10].findIndex((n) => n < 0); // 返回索引2
let result4 = [1, 4, -5, 10,NaN].findIndex((n) => Object.is(NaN, n)); // 返回索引4

keys & values & entries

keys() & values() & entries() 遍历键名、遍历键值、遍历键名+键值。
三个方法都返回一个新的 Array Iterator 对象,对象根据方法不同包含不同的值。

语法:
array.keys()
array.values()
array.entries()

for (let index of ['a', 'b'].keys()) {
 console.log(index);
}
// 0
// 1
 
for (let elem of ['a', 'b'].values()) {
 console.log(elem);
}
// 'a'
// 'b'
 
for (let [index, elem] of ['a', 'b'].entries()) {
 console.log(index, elem);
}
// 0 "a"
// 1 "b"for..of中如果遍历中途要退出,可以使用break退出循环。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值