JS中操作数组的方法或者属性(20个)

86 阅读3分钟

在JavaScript中,有许多用于操作数组的方法和属性。以下是其中的20个示例:

  1. length - 返回数组的长度。 示例:

    const arr = [1, 2, 3, 4, 5];
    console.log(arr.length); // 输出: 5
    
  2. push() - 向数组末尾添加一个或多个元素,并返回新的长度。 示例:

    const arr = [1, 2, 3];
    arr.push(4);
    console.log(arr); // 输出: [1, 2, 3, 4]
    
  3. pop() - 移除并返回数组的最后一个元素。 示例:

    const arr = [1, 2, 3];
    const removedElement = arr.pop();
    console.log(removedElement); // 输出: 3
    console.log(arr); // 输出: [1, 2]
    
  4. shift() - 移除并返回数组的第一个元素。 示例:

    const arr = [1, 2, 3];
    const removedElement = arr.shift();
    console.log(removedElement); // 输出: 1
    console.log(arr); // 输出: [2, 3]
    
  5. unshift() - 向数组的开头添加一个或多个元素,并返回新的长度。 示例:

    const arr = [1, 2, 3];
    arr.unshift(0);
    console.log(arr); // 输出: [0, 1, 2, 3]
    
  6. concat() - 连接两个或更多数组,并返回结果数组。 示例:

    const arr1 = [1, 2];
    const arr2 = [3, 4];
    const newArr = arr1.concat(arr2);
    console.log(newArr); // 输出: [1, 2, 3, 4]
    
  7. slice() - 从数组中提取指定位置的元素,并返回新的数组。 示例:

    const arr = [1, 2, 3, 4, 5];
    const newArr = arr.slice(2, 4);
    console.log(newArr); // 输出: [3, 4]
    
  8. splice() - 在指定位置添加或删除元素,并返回被删除的元素。 示例:

    const arr = [1, 2, 3, 4, 5];
    const removedElements = arr.splice(1, 2, 6, 7);
    console.log(arr); // 输出: [1, 6, 7, 4, 5]
    console.log(removedElements); // 输出: [2, 3]
    
  9. reverse() - 颠倒数组中元素的顺序。 示例:

    const arr = [1, 2, 3, 4, 5];
    arr.reverse();
    console.log(arr); // 输出: [5, 4, 3, 2, 1]
    
  10. sort() - 对数组元素进行排序。 示例:

    const arr = [3, 1, 2, 5,]
    arr.sort();
    console.log(arr); // 输出: [1, 2, 3, 4, 5]
    
  11. indexOf() - 返回指定元素在数组中第一次出现的索引,如果不存在则返回-1。 示例:

    const arr = [1, 2, 3, 4, 5];
    const index = arr.indexOf(3);
    console.log(index); // 输出: 2
    
  12. lastIndexOf() - 返回指定元素在数组中最后一次出现的索引,如果不存在则返回-1。 示例:

    const arr = [1, 2, 3, 2, 1];
    const index = arr.lastIndexOf(2);
    console.log(index); // 输出: 3
    
  13. includes() - 判断数组是否包含指定元素,返回布尔值。 示例:

    const arr = [1, 2, 3, 4, 5];
    const isIncluded = arr.includes(3);
    console.log(isIncluded); // 输出: true
    
  14. forEach() - 对数组中的每个元素执行提供的函数。 示例:

    const arr = [1, 2, 3];
    arr.forEach(element => {
      console.log(element); // 输出: 1, 2, 3 (分别在不同行)
    });
    
  15. map() - 创建一个新数组,其结果是对原数组中的每个元素调用提供的函数后的返回值。 示例:

    const arr = [1, 2, 3];
    const newArr = arr.map(element => element * 2);
    console.log(newArr); // 输出: [2, 4, 6]
    
  16. filter() - 创建一个新数组,其中包含所有通过提供函数的测试的元素。 示例:

    const arr = [1, 2, 3, 4, 5];
    const newArr = arr.filter(element => element > 2);
    console.log(newArr); // 输出: [3, 4, 5]
    
  17. reduce() - 对数组中的元素执行一个函数,将其结果累加到单个值。 示例:

    const arr = [1, 2, 3, 4, 5];
    const sum = arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
    console.log(sum); // 输出: 15
    
  18. some() - 检测数组中是否至少有一个元素满足提供的函数的测试。 示例:

    const arr = [1, 2, 3, 4, 5];
    const hasEvenNumber = arr.some(element => element % 2 === 0);
    console.log(hasEvenNumber); // 输出: true
    
  19. every() - 检测数组中是否所有元素都满足提供的函数的测试。 示例:

    const arr = [1, 2, 3, 4, 5];
    const allPositiveNumbers = arr.every(element => element > 0);
    console.log(allPositiveNumbers); // 输出: true
    
  20. join() - 将数组中的所有元素以指定的分隔符连接成一个字符串。 示例:

    const arr = [1, 2, 3];
    const joinedString = arr.join('-');
    console.log(joinedString); // 输出: "1-2-3"
    

以上是一些常用的数组方法和属性, 可以帮助您在JavaScript中对数组进行各种操作和处理。