function has(list, item) {
list.forEach(v => {
if (v === item) {
return true
}
})
return false
}
console.info(has([1,2,3], 1))
怎么让结果为true???
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
1、forEach循环无法中断
2、用for循环
3、用indexOf
4、用ES6的includes
forEach
方法无法中断执行,总是会将所有成员遍历完可以这样
或者用
for
循环箭头函数没有自己的this值,而是继承自外围作用域。
其实题主并不是不知道用其他方式来实现这个简单的功能, 只是用forEach来做
因为forEach的
return true
和return false
并不是返回值的作用, 所有只能借助flag