JavaScript高级函数【部分】

1.every根据条件是否符合返回布尔值

有一个不符合就返回false,与 && 的判断逻辑相似

var arr = [1, 6, -2, 5, 8];
console.log(arr.every(item => item > 0));
// false

2.some根据条件返回布尔值

有一个符合就返回true,与 || 的判断逻辑相似

var arr = [1, 6, -2, 5, 8];
console.log(arr.some(item => item > 0));
// true

3.findfindIndex

返回第一个符合条件的元素或索引,不会放在数组中返回

var arr = [1, 6, -2, 5, 8];
let fushu = arr.find(item => item < 0)
let fushuIndex = arr.findIndex(item => item < 0)
console.log('小于0的数为:', fushu);
console.log('小于0的数索引值:', fushuIndex);
// 小于0的数为: -2
// 小于0的数索引值: 2
发表评论 / Comment

用心评论~