问题:只想循环一次就return退出循环,但return 不生效
let arr = [1, 2];
let a = 0;
arr.forEach((it) => {
a = a + it;
return;
});
console.log('a ==:', a); //3
解决方法:
将Api换成some
let arr = [1, 2];
let a = 0;
arr.some((it) => {
a = a + it;
return true;
});
console.log('a ==:', a); //1