开心每一天1111
因为这就是它的设计目的 - 它将始终循环遍历数组中的所有项目,就像大多数其他数组方法一样forEach,例如和map。如果过了某个时间点,您想有效地忽略之后发生的事情,只需返回当前累加器,例如:const partialSum = [1, 2, 3, 4].reduce((a, num) => { if (a > 3) { return a; } return a + num;}, 0);console.log(partialSum);如果你想在数组中找到一个元素,你应该使用.find,它会在找到匹配项后立即终止循环:const arr = [ { prop: 'foo' }, { prop: 'bar' }, { prop: 'baz' }];const foundBar = arr.find((obj) => { console.log('iteration'); return obj.prop === 'bar';});Athrow 确实会跳出 a .reduce(尽管您不应该使用错误进行控制流):[1, 2, 3, 4, 5].reduce((a, num) => { console.log('iteration'); if (num > 2) { throw new Error(); }});