关于遍历的12种方法
ES5:
forEach、every 、some、 filter、map、reduce、reduceRight、
ES6:
find、findIndex、keys、values、entries
forEach 按升序为数组中含有效值的每一项执行一次回调函数
array.forEach(function(currentValue, index, arr), thisValue)
关于forEach()
- 无法中途退出循环,只能用return退出本次回调,进行下一次回调。
- 它总是返回 undefined值,即使你return了一个值。
eg:
let a = [1, 2, ,3]; // 最后第二个元素是空的,不会遍历(undefined、null会遍历)
let obj = { name: 'Avegetable' };
let result = a.forEach(function (value, index, array) {
a[3] = '改变元素';
a.push('添加到尾端,不会被遍历')
console.log(value, 'forEach传递的第一个参数'); // 分别打印 1 ,2 ,改变元素
console.log(this.name); // Avegetable 打印三次 this绑定在obj对象上
// break; // break会报错
return value; // return只能结束本次回调 会执行下次回调
console.log('不会执行,因为return 会执行下一次循环回调')
}, obj);
console.log(result); // 即使return了一个值,也还是返回undefined
// 回调函数也接受接头函数写法
every 检测数组所有元素是否都符合判断条件
array.every(function(currentValue, index, arr), thisValue)
关于every()
- 如果数组中检测到有一个元素不满足,则整个表达式返回 false,且剩余的元素不会再进行检测。
- 如果所有元素都满足条件,则返回 true。
eg:
function isBigEnough(element, index, array) {
return element >= 10; // 判断数组中的所有元素是否都大于10
}
let result = [12, 5, 8, 130, 44].every(isBigEnough); // false
let result = [12, 54, 18, 130, 44].every(isBigEnough); // true
// 接受箭头函数写法
[12, 5, 8, 130, 44].every(x => x >= 10); // false
[12, 54, 18, 130, 44].every(x => x >= 10); // true
some 数组中的是否有满足判断条件的元素
array.some(function(currentValue, index, arr), thisValue)
关于some()
- 如果有一个元素满足条件,则表达式返回true, 剩余的元素不会再执行检测。
- 如果没有满足条件的元素,则返回false。
eg:
function isBigEnough(element, index, array) {
return (element >= 10); //数组中是否有一个元素大于 10
}
let result = [2, 5, 8, 1, 4].some(isBigEnough); // false
let result = [12, 5, 8, 1, 4].some(isBigEnough); // true
filter 过滤原始数组,返回一个新数组, 其包含通过所提供函数实现的测试的所有元素
let new_array = arr.filter(function(currentValue, index, arr), thisArg)
eg:
let a = [32, 33, 16, 40];
let result = a.filter(function (value, index, array) {
return value >= 18; // 返回a数组中所有大于18的元素
});
console.log(result,a);// [32,33,40] [32,33,16,40]
map 创建一个新数组,结果是该数组中的每个元素都调用一个提供的函数后返回的结果
let new_array = arr.map(function(currentValue, index, arr), thisArg)
eg:
let a = ['1','2','3','4'];
let result = a.map(function (value, index, array) {
return value + '新数组的新元素'
});
console.log(result, a);
// ["1新数组的新元素","2新数组的新元素","3新数组的新元素","4新数组的新元素"] ["1","2","3","4"]
reduce 对累加器和数组中的每个元素(从左到右)应用一个函数,最终合并为一个值。
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
参数:
total(必须)
,初始值, 或者上一次调用回调返回的值currentValue(必须)
,数组当前元素的值index(可选)
, 当前元素的索引值arr(可选)
,数组对象本身initialValue(可选)
: 指定第一次回调 的第一个参数
回调第一次执行时:
- 如果
initialValue
在调用reduce
时被提供,那么第一个total
将等于initialValue
,此时currentValue
等于数组中的第一个值; - 如果
initialValue
未被提供,那么total
等于数组中的第一个值,currentValue
等于数组中的第二个值。此时如果数组为空,那么将抛出TypeError
。 - 如果数组仅有一个元素,并且没有提供
initialValue
,或提供了initialValue
但数组为空,那么回调不会被执行,数组的唯一值将被返回。
eg:
// 数组求和
let sum = [0, 1, 2, 3].reduce(function (a, b) {
return a + b;
}, 0);
// 6
// 将二维数组转化为一维 将数组元素展开
let flattened = [[0, 1], [2, 3], [4, 5]].reduce(
(a, b) => a.concat(b),
[]
);
// [0, 1, 2, 3, 4, 5]
reduceRight 方法除了与reduce执行方向相反外,其他完全与其一致。
find 用于找出第一个符合条件的数组成员,并返回该成员,如果没有符合条件的成员,则返回undefined。
let item = arr.find(function(currentValue, index, arr), thisArg)
eg:
let a = [1, 4, -5, 10].find((n) => n < 0); // 返回元素-5
let b = [1, 4, -5, 10,NaN].find((n) => Object.is(NaN, n)); // 返回元素NaN
findIndex 返回第一个符合条件的数组成员的位置,如果所有成员都不符合条件,则返回-1。
let index = arr.findIndex(function(currentValue, index, arr), thisArg)
eg:
let a = [1, 4, -5, 10].findIndex((n) => n < 0); // 返回索引2
let b = [1, 4, -5, 10,NaN].findIndex((n) => Object.is(NaN, n)); // 返回索引4
keys(),values(),entries() 遍历键名、遍历键值、遍历键名+键值。三个方法都返回一个新的 Array Iterator 对象,对象根据方法不同包含不同的值。
array.keys()
array.values()
array.entries()
eg:
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
退出循环。 -
如果不使用
for...o
f循环,可以手动调用遍历器对象的next
方法,进行遍历
let letter = ['a', 'b', 'c'];
let entries = letter.entries();
console.log(entries.next().value); // [0, 'a']
console.log(entries.next().value); // [1, 'b']
console.log(entries.next().value); // [2, 'c']