• js数组方法复习汇总


    1. push() 和 pop()

    push() 方法向数组末尾添加一个或多个元素,并返回新的长度。

    const fruits = ['apple', 'banana'];
    const length = fruits.push('orange', 'grape');
    console.log(fruits); // 输出: ['apple', 'banana', 'orange', 'grape']
    console.log(length); // 输出: 4
    
    • 1
    • 2
    • 3
    • 4

    pop() 方法从数组末尾移除最后一个元素,并返回该元素的值。

    const fruits = ['apple', 'banana', 'orange'];
    const lastFruit = fruits.pop();
    console.log(fruits); // 输出: ['apple', 'banana']
    console.log(lastFruit); // 输出: 'orange'
    
    • 1
    • 2
    • 3
    • 4

    2. shift() 和 unshift()

    shift() 方法从数组开头移除第一个元素,并返回该元素的值。

    const fruits = ['apple', 'banana', 'orange'];
    const firstFruit = fruits.shift();
    console.log(fruits); // 输出: ['banana', 'orange']
    console.log(firstFruit); // 输出: 'apple'
    
    • 1
    • 2
    • 3
    • 4

    unshift() 方法向数组开头添加一个或多个元素,并返回新的长度。

    const fruits = ['banana', 'orange'];
    const length = fruits.unshift('apple', 'grape');
    console.log(fruits); // 输出: ['apple', 'grape', 'banana', 'orange']
    console.log(length); // 输出: 4
    
    • 1
    • 2
    • 3
    • 4

    3. concat()

    concat() 方法用于合并两个或多个数组,并返回一个新数组。

    const fruits1 = ['apple', 'banana'];
    const fruits2 = ['orange', 'grape'];
    const allFruits = fruits1.concat(fruits2);
    console.log(allFruits); // 输出: ['apple', 'banana', 'orange', 'grape']
    
    • 1
    • 2
    • 3
    • 4

    4. slice()

    slice() 方法返回一个新数组,其中包含从开始索引到结束索引(不包括结束索引)的选定元素。

    const fruits = ['apple', 'banana', 'orange', 'grape'];
    const slicedFruits = fruits.slice(1, 3);
    console.log(slicedFruits); // 输出: ['banana', 'orange']
    
    • 1
    • 2
    • 3

    5. splice()

    splice() 方法用于插入、删除或替换数组的元素,并返回被删除的元素。

    const fruits = ['apple', 'banana', 'orange', 'grape'];
    const removedFruits = fruits.splice(1, 2, 'kiwi', 'strawberry');
    console.log(fruits); // 输出: ['apple', 'kiwi', 'strawberry', 'grape']
    console.log(removedFruits); // 输出: ['banana', 'orange']
    
    • 1
    • 2
    • 3
    • 4

    6. forEach()

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

    const fruits = ['apple', 'banana', 'orange'];
    fruits.forEach(function(fruit) {
      console.log(fruit);
    });
    // 输出:
    // 'apple'
    // 'banana'
    // 'orange'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    7. map()

    map() 方法创建一个新数组,其中包含对原始数组中的每个元素应用提供的函数的结果。

    const numbers = [1, 2, 3];
    const doubledNumbers = numbers.map(function(number) {
      return number * 2;
    });
    console.log(doubledNumbers); // 输出: [2, 4, 6]
    
    • 1
    • 2
    • 3
    • 4
    • 5

    8. filter()

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

    const numbers = [1, 2, 3, 4, 5];
    const evenNumbers = numbers.filter(function(number) {
      return number % 2 === 0;
    });
    console.log(evenNumbers); // 输出: [2, 4]
    
    • 1
    • 2
    • 3
    • 4
    • 5

    9. reduce()

    reduce() 方法对数组中的每个元素(从左到右)应用一个函数,将其减少为单个值。

    const numbers = [1, 2, 3, 4, 5];
    const sum = numbers.reduce(function(total, number) {
      return total + number;
    }, 0);
    console.log(sum); // 输出: 15
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    Java│蓝桥杯省赛真题星期一问题
    在家怎么做芋圆 芋圆的做法
    Python爬虫实战(实战篇)—17获取【CSDN某一专栏】数据转为Markdown列表放入文章中
    Synchronized面试题
    Java进阶学习笔记31——日期时间
    23种设计模式之代理模式(动态代理)
    JDBC整合C3P0,DBCP,DRUID数据库连接池
    光电二极管放大应用方案
    24. Kernel 4.19环境下,Cilium网络仍然需要使用iptables
    【Git】常见工作场景
  • 原文地址:https://blog.csdn.net/qq_42431718/article/details/134005347