函数特点
- 返回undefined
- 循环索引只能从0开始,无法干预
- 若forEach的回调函数是箭头函数,this参数会被忽略
- 在forEach的回调函数中使用continue会报错,若要实现continue的功能,可以用return来替代
const ary = [1,2,3,4,5,6]
ary.forEach((item, index) => {
if (index < 3) return
console.log(index)
})
const ary = [1,,3,4]
ary.length = 4
let sum = 0
ary.forEach((item) => {
console.log(item)
sum++
})
console.log(sum)
- 除了抛出异常,无法中止或跳出循环【暴力实现break效果】
const ary = [1,2,3,4];
try {
ary.forEach((item, index) => {
if (index === 2) throw index;
});
} catch(err) {
console.log(`当下标来到${err}时停止循环`);
}
函数用法
Array.prototype.forEach(function(element , index, array) => {}, this);
const ary = [1,2,3,4,5,6,7];
ary.forEach((element, index, array) => {
console.log(element, index, array, this);
}, this);
使用场景
- 遍历数组内容
- 扁平化数组【当然也可以使用Array.prorotype.flat()】
const flatFn = (ary) => {
const result = []
ary.forEach((item) => {
if (Array.isArray(item)) {
result.push(...flatFn(item))
} else {
result.push(item)
}
})
return result
}
const ary = [[1, [2]], [3,4,[5]], [6, [[7]]]]
console.log(flatFn(ary))
手写实现
Array.prototype.myForEach = function(callback, thisArg) {
if (this === null || this === undefined) {
throw new TypeError('this is null or undefined');
}
if (typeof callback !== 'function') {
throw new TypeError(callback + 'is not a function');
}
const obj = Object(this);
const len = obj.length >>> 0;
let i = 0;
while (i < len) {
if (i in obj) {
callback.call(thisArg, obj[i], i, obj);
}
i++;
}
}