Array.prototype.map()
示例代码
//将创建一个新数组, 由原数组中每个元素都调用一次提供的函数后的返回值组成.
//callbackFunc 是数组中的每个元素执行的函数,它的返回值作为一个元素被添加到新数组中。
// callbackFunc函数被每次循环调用时将传入以下参数:
//- item 数组中当前正在处理的元素。
//- index 正在处理的元素在数组中的索引。
//- array 调用了 `map()` 的数组本身。
// 返回值: 一个新数组,每个元素都是回调函数的返回值。
const arr = [1, 4, 9, 16];
const resArr = arr.map((x) => x * 2);
console.log(resArr); //结果: [2, 8, 18, 32]
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