isDone 是否遍历完成
const arr1 = [1, 2, 3];
let isDone = forEach(arr1, (_v, k) => (arr1[k] = k));
isEqual(arr1, [0, 1, 2]); // true
isDone; // true
// ArrayLike
isDone = forEach({ 0: 1, 1: 2, length: 2 }, (_v, k) => (arr1[k] = k + k));
isEqual(arr1, [0, 2, 2]); // true
isDone; // true
isDone = forEach(arr1, (_v, k) => {
arr1[k] = k + 1;
return k !== 1;
});
isDone; // false
isEqual(arr1, [1, 2, 2]); // true
let elseCount = 0;
isDone = forEach(
arr2,
(v, k) => (arr2[k] = 'a' + v),
() => elseCount++, // 完整遍历以后会执行该方法
);
isDone; // true
elseCount; // 1
遍历数组,并不完全等同 Array.prototype.forEach,该forEach支持中断,支持类似Python的for else功能
Python的for else hack写法