Array.prototype.some()
示例代码
// 数组相关方法----Array.prototype.some() 为数组中的每个元素执行func函数
//func函数的参数: item当前循环的元素,index当前循环的索引,array当前调用some的数组本身
//判断数组中是否至少有一个元素是偶数
const array = [1, 2, 1, 3, 5];
// const func = (e) => e % 2 === 0;
const res = array.some(e => e % 2 === 0);
console.log(res);
Array.prototype.every()
示例代码
//数组相关方法----Array.prototype.every()
// 检测数组内所有元素是否都能通过指定函数的测试,返回一个布尔值
const array1 = [1, 30, 39, 29, 10, 13];
//循环检测array1中每个元素是否都小于40
const res=array1.every( item=> item<40 );
console.log(res); //true