MDN 的例子是这样的:
const isBelowThreshold = (currentValue) => currentValue < 40;
const array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold));
// expected output: true
我自己写的时候出了个小问题
我的代码:
// 错误代码
const arr = [{
value: 1
}, {
value: 1
}, {
value: 4
}, {
value: 3
}, {
value: 2
}, ]
const flag = arr.every(item => {
item.value < 10
})
console.log(flag);
结果 flag 打印出来是 false
正确代码应该是这样:
// 正确代码
const arr = [{
value: 1
}, {
value: 1
}, {
value: 4
}, {
value: 3
}, {
value: 2
}, ]
const flag = arr.every(item => {
- item.value < 10
+ return item.value < 10
})
console.log(flag);