1. filter()

filter() 方法 创建一个新数组 ,其中包含通过提供的函数实现的测试的所有元素

const words = ['HTML', 'CSS', 'JavaScript', 'Python', 'Java'];
const longWords = words.filter(word => word.length > 4);
console.log(longWords); // ["JavaScript", "Python"]           

2. forEach()

forEach() 方法为每个数组元素执行一次提供的函数

const words = ['HTML', 'CSS', 'JavaScript'];
words.forEach(word => console.log(word));
// HTML
// CSS
// JavaScript           

3. some()

some() 方法测试数组中是否至少有一个元素通过了提供的函数实现的测试。它返回一个布尔值

const myArray = [1, 2, 3, 4, 5];
const evenExists = myArray.some(element => element % 2 === 0);
console.log(evenExists); // true           

4. every()

every() 方法测试数组中的所有元素是否通过提供的函数实现的测试。它返回一个布尔值

const myArray = [1, 2, 3, 4, 5];
const allEven = myArray.every(element => element % 2 === 0);
console.log(allEven); // false           

5. includes()

includes() 方法确定数组是否在其条目中包含某个值,返回 truefalse 视情况而定

const numbers = [1, 2, 3, 4, 5];
console.log(numbers.includes(2)); // true
console.log(numbers.includes(8)); // false          

6. map()

map() 方法 创建一个新数组,其中 填充了对调用数组中的每个元素调用提供的函数的结果

const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(x => x * 2);
console.log(doubled);           

7. reduce()

reduce() 方法对数组的每个元素执行一个 reducer函数(您提供的),从而产生单个输出值

const numbers = [10, 20, 30, 40];
const sum = numbers.reduce((x, y) => x + y, 0);
console.log(sum); // 100           

8. sort()

sort()方法对数组的元素进行原地排序并返回排序后的数组。默认排序顺序是升序,建立在将元素转换为字符串,然后比较它们的 UTF-16 代码单元值序列的基础上

const techs = ['HTML', 'CSS', 'JavaScript'];
techs.sort();
console.log(techs); // ["CSS", "HTML", "JavaScript"]
const numbers = [1, 30, 4, 21, 100000];
numbers.sort((x, y) => x - y);
console.log(numbers); // [1, 4, 21, 30, 100000]           

9. find()

find()方法返回 提供的数组中满足提供的测试函数 的 第一个元素的值

const numbers = [7, 14, 8, 128, 56];
const found = numbers.find(element => element > 10);
console.log(found); // 14           

10. findIndex()

findIndex() 方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1,表示没有元素通过测试

const numbers = [5, 12, 8, 130, 44];
const indexFound = numbers.findIndex(element => element > 15);
console.log(indexFound); // 3           
点赞(0)

评论列表 共有 0 评论

暂无评论

微信服务号

微信客服

淘宝店铺

support@elephdev.com

发表
评论
Go
顶部