2.JavaScript数组方法

53 阅读4分钟

JavaScript数组带有许多内置方法,允许您对其执行各种操作。以下是一些最常用的数组方法的示例:

  1. ​push()​-将一个或多个元素添加到数组的末尾并返回数组的新长度

    let myArray = [1, 2, 3];
    myArray.push(4, 5);
    console.log(myArray); // [1, 2, 3, 4, 5]
    
  2. ​pop()​-从数组中删除最后一个元素并返回该元素

    let myArray = [1, 2, 3];
    let lastElement = myArray.pop();
    console.log(myArray); // [1, 2]
    console.log(lastElement); // 3
    
  3. ​shift()​ -从数组中删除第一个元素并返回该元素

    let myArray = [1, 2, 3];
    let firstElement = myArray.shift();
    console.log(myArray); // [2, 3]
    console.log(firstElement); // 1
    
  4. ​unshift()​ -将一个或多个元素添加到数组的开头并返回数组的新长度

    let myArray = [1, 2, 3];
    myArray.unshift(0);
    console.log(myArray); // [0, 1, 2, 3]
    
  5. ​slice()​ -将数组的一部分的浅拷贝返回到从头到尾选择的新数组对象中。原始数组不会被修改。

    let myArray = [1, 2, 3, 4, 5];
    let removed = myArray.splice(1, 2);
    console.log(myArray); // [1, 4, 5]
    console.log(removed); // [2, 3]
    
  6. ​splice()​- 从数组中添加/删除元素并返回删除的元素。

    let myArray = [1, 2, 3, 4, 5];
    let removed = myArray.splice(1, 2);
    console.log(myArray); // [1, 4, 5]
    console.log(removed); // [2, 3]
    
  7. ​sort()​ -对数组的元素进行排序并返回排序后的数组

    let myArray = [3, 1, 5, 2, 4];
    myArray.sort();
    console.log(myArray); // [1, 2, 3, 4, 5]
    
  8. ​filter()​-创建一个新数组,其中包含通过提供的函数实现的测试的所有元素

    let myArray = [1, 2, 3, 4, 5, 6];
    let filtered = myArray.filter(n => n > 3);
    console.log(filtered); // [4, 5, 6]
    
  9. ​map()​-创建一个新数组,其结果是对调用数组中的每个元素调用提供的函数

    let myArray = [1, 2, 3, 4, 5];
    let squared = myArray.map(n => n * n);
    console.log(squared); // [1, 4, 9, 16, 25]
    
  10. ​concat()​ -返回一个新数组,该数组是原始数组和一个或多个附加数组或值的连结

    let myArray1 = [1, 2, 3];
    let myArray2 = [4, 5, 6];
    let combined = myArray1.concat(myArray2);
    console.log(combined); // [1, 2, 3, 4, 5, 6]
    
  11. ​indexOf()​ -返回可以在数组中找到给定元素的第一个索引,如果不存在,则返回-1

    let myArray = [1, 2, 3, 4, 5];
    let index = myArray.indexOf(3);
    console.log(index); // 2
    
  12. ​join()​-将数组的所有元素连接到一个字符串中并返回生成的字符串。默认情况下,它用逗号分隔所有元素。

    let myArray = [1, 2, 3, 4, 5];
    let myString = myArray.join();
    console.log(myString); // "1,2,3,4,5"
    
  13. ​reduce()​ -对累加器和数组中的每个元素(从左到右)应用一个函数以将其减少为单个值。

    let myArray = [1, 2, 3, 4, 5];
    let sum = myArray.reduce((accumulator, currentValue) => accumulator + currentValue);
    console.log(sum); // 15
    
  14. ​reduceRight()​ -对累加器和数组的每个值(从右到左)应用一个函数,以将其减少为单个值。

    let myArray = [1, 2, 3, 4, 5];
    let sum = myArray.reduceRight((accumulator, currentValue) => accumulator + currentValue);
    console.log(sum); // 15
    
  15. ​forEach()​-按顺序为数组中的每个元素调用一次提供的函数

    let myArray = [1, 2, 3, 4, 5];
    myArray.forEach(n => console.log(n));
    // Outputs: 1, 2, 3, 4, 5
    
  16. ​every()​-测试数组中的所有元素是否都通过了提供的函数实现的测试

    let myArray = [1, 2, 3, 4, 5];
    let isAllPositive = myArray.every(n => n > 0);
    console.log(isAllPositive); // true
    
  17. ​find()​ -返回数组中满足提供的测试函数的第一个元素的值。否则返回undefined

    let myArray = [1, 2, 3, 4, 5];
    let found = myArray.find(n => n > 3);
    console.log(found); // 4
    
  18. ​findIndex()​ -返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1

    let myArray = [1, 2, 3, 4, 5];
    let foundIndex = myArray.findIndex(n => n > 3);
    console.log(foundIndex); // 3
    
  19. ​includes()​ -确定数组是否在其条目中包含某个值,酌情返回true或false。

    let myArray = [1, 2, 3, 4, 5];
    let hasValue = myArray.includes(3);
    console.log(hasValue); // true
    
  20. ​flat()​ -创建一个新数组,其中所有子数组元素递归连接到指定的深度。

    let myArray = [1, [2, 3], [4, [5, 6]]];
    let flatArray = myArray.flat(2);
    console.log(flatArray); // [1, 2, 3, 4, 5, 6]
    
  21. ​flatMap()​ -首先使用映射函数映射每个元素,然后将结果展平为一个新数组

    let myArray = [1, 2, 3, 4, 5];
    let mappedArray = myArray.flatMap(n => [n, n*2]);
    console.log(mappedArray); // [1, 2, 2, 4, 3, 6, 4, 8, 5, 10]
    
  22. ​copyWithin()​ -浅层将数组的一部分复制到同一数组中的另一个位置并返回它而不修改其长度

    let myArray = [1, 2, 3, 4, 5];
    myArray.copyWithin(0, 2);
    console.log(myArray); // [3, 4, 5, 4, 5]
    
  23. ​reverse()​ -颠倒数组中元素的顺序并返回颠倒的数组。

    let myArray = [1, 2, 3, 4, 5];
    myArray.reverse();
    console.log(myArray); // [5, 4, 3, 2, 1]
    
  24. ​at()​-该方法允许正整数和负整数作为索引从数组的前端和末尾返回值。

    let myArray = [1, 2, 3, 4, 5];
    myArray.at(2); // Returns 3
    myArray.at(-2); // Returns 4
    

进一步阅读

  • 阅读MDN指南中有关数组及其内置方法的更多信息。