跳到主要内容

js 数组有哪些方法?

常用方法

JavaScript 数组提供了许多内置方法,用于操作和处理数组数据。以下是一些常用的数组方法及其简要说明:

1. 添加和删除元素

  • push(element):在数组末尾添加一个或多个元素,并返回新的长度。

    const arr = [1, 2, 3];
    arr.push(4); // arr -> [1, 2, 3, 4], 返回 4
  • pop():移除数组最后一个元素,并返回该元素。

    const arr = [1, 2, 3];
    const last = arr.pop(); // arr -> [1, 2], last -> 3
  • unshift(element):在数组开头添加一个或多个元素,并返回新的长度。

    const arr = [1, 2, 3];
    arr.unshift(0); // arr -> [0, 1, 2, 3], 返回 4
  • shift():移除数组第一个元素,并返回该元素。

    const arr = [1, 2, 3];
    const first = arr.shift(); // arr -> [2, 3], first -> 1
  • splice(start, deleteCount, item1, ...):从指定位置添加或删除元素。

    const arr = [1, 2, 3, 4, 5];
    arr.splice(2, 1); // arr -> [1, 2, 4, 5], 删除索引 2 的元素
    arr.splice(2, 0, 3, 6); // arr -> [1, 2, 3, 6, 4, 5], 在索引 2 处插入 3 和 6

2. 查找和过滤

  • indexOf(searchElement, fromIndex):返回指定元素在数组中的第一个索引,如果不存在则返回 -1。

    const arr = [1, 2, 3, 4, 5];
    const index = arr.indexOf(3); // index -> 2
  • lastIndexOf(searchElement, fromIndex):返回指定元素在数组中的最后一个索引,如果不存在则返回 -1。

    const arr = [1, 2, 3, 2, 5];
    const lastIndex = arr.lastIndexOf(2); // lastIndex -> 3
  • includes(searchElement, fromIndex):检查数组是否包含指定元素,返回布尔值。

    const arr = [1, 2, 3, 4, 5];
    const includes = arr.includes(3); // includes -> true
  • find(callback, thisArg):返回数组中满足提供的测试函数的第一个元素的值,否则返回 undefined

    const arr = [1, 2, 3, 4, 5];
    const found = arr.find((element) => element > 3); // found -> 4
  • findIndex(callback, thisArg):返回数组中满足提供的测试函数的第一个元素的索引,否则返回 -1。

    const arr = [1, 2, 3, 4, 5];
    const foundIndex = arr.findIndex((element) => element > 3); // foundIndex -> 3
  • filter(callback, thisArg):创建一个新数组,包含所有满足提供的测试函数的元素。

    const arr = [1, 2, 3, 4, 5];
    const filtered = arr.filter((element) => element % 2 === 0); // filtered -> [2, 4]

3. 映射和转换

  • map(callback, thisArg):创建一个新数组,其结果是调用提供的函数处理每个元素的结果。

    const arr = [1, 2, 3, 4, 5];
    const mapped = arr.map((element) => element * 2); // mapped -> [2, 4, 6, 8, 10]
  • flatMap(callback, thisArg):映射每个元素,然后展平结果数组。

    const arr = [1, 2, 3];
    const flatMapped = arr.flatMap((x) => [x, x * 2]); // flatMapped -> [1, 2, 2, 4, 3, 6]
  • reduce(callback, initialValue):对数组中的每个元素执行一个由您提供的 reducer 函数(升序执行),将其结果汇总为单个返回值。

    const arr = [1, 2, 3, 4, 5];
    const sum = arr.reduce((acc, curr) => acc + curr, 0); // sum -> 15
  • reduceRight(callback, initialValue):与 reduce 类似,但按降序执行。

    const arr = [1, 2, 3, 4, 5];
    const sum = arr.reduceRight((acc, curr) => acc + curr, 0); // sum -> 15

4. 排序和反转

  • sort(compareFunction):对数组元素进行排序,原地修改数组。

    const arr = [3, 1, 4, 1, 5, 9];
    arr.sort(); // arr -> [1, 1, 3, 4, 5, 9]
  • reverse():反转数组中的元素,原地修改数组。

    const arr = [1, 2, 3, 4, 5];
    arr.reverse(); // arr -> [5, 4, 3, 2, 1]

5. 拼接和分割

  • concat(...arrays):将一个或多个数组连接到当前数组,返回一个新数组。

    const arr1 = [1, 2, 3];
    const arr2 = [4, 5, 6];
    const concatenated = arr1.concat(arr2); // concatenated -> [1, 2, 3, 4, 5, 6]
  • join(separator):将数组的所有元素连接成一个字符串,并返回该字符串。

    const arr = [1, 2, 3, 4, 5];
    const joined = arr.join(", "); // joined -> "1, 2, 3, 4, 5"
  • slice(start, end):返回从开始到结束(不包括结束)选择的数组的一部分浅拷贝,原数组不变。

    const arr = [1, 2, 3, 4, 5];
    const sliced = arr.slice(1, 3); // sliced -> [2, 3]

6. 其他方法

  • every(callback, thisArg):测试数组的所有元素是否都通过提供的函数测试,返回布尔值。

    const arr = [1, 2, 3, 4, 5];
    const allEven = arr.every((element) => element % 2 === 0); // allEven -> false
  • some(callback, thisArg):测试数组中是否有至少一个元素通过提供的函数测试,返回布尔值。

    const arr = [1, 2, 3, 4, 5];
    const anyEven = arr.some((element) => element % 2 === 0); // anyEven -> true
  • fill(value, start, end):用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。

    const arr = [1, 2, 3, 4, 5];
    arr.fill(0, 2, 4); // arr -> [1, 2, 0, 0, 5]
  • copyWithin(target, start, end):浅复制数组的一部分到同一数组中的另一个位置,并返回它,不会改变原数组的长度。

    const arr = [1, 2, 3, 4, 5];
    arr.copyWithin(0, 3, 5); // arr -> [4, 5, 3, 4, 5]
  • entries():返回一个数组迭代器对象,该对象包含了数组中每个索引的键值对。

    const arr = [1, 2, 3];
    const iterator = arr.entries();
    console.log(iterator.next().value); // [0, 1]
    console.log(iterator.next().value); // [1, 2]
  • keys():返回一个数组迭代器对象,该对象包含了数组中每个索引的键。

    const arr = [1, 2, 3];
    const iterator = arr.keys();
    console.log(iterator.next().value); // 0
    console.log(iterator.next().value); // 1
  • values():返回一个数组迭代器对象,该对象包含了数组中每个索引的值。

    const arr = [1, 2, 3];
    const iterator = arr.values();
    console.log(iterator.next().value); // 1
    console.log(iterator.next().value); // 2

静态方法

4 个静态方法

  • Array.from()
  • Array.fromAsync()
  • Array.isArray()
  • Array.of()

总结

31 个

JavaScript 数组提供了丰富的内置方法,用于各种操作和处理。通过合理使用这些方法,可以极大地简化代码并提高开发效率。如果你有任何更具体的问题或需要进一步的解释,请告诉我!

还有吗?

当然,JavaScript 数组还提供了其他一些有用的方法。以下是更多数组方法及其简要说明:

7. 遍历和迭代

  • forEach(callback, thisArg):对数组的每个元素执行一次提供的函数,不返回任何值。

    const arr = [1, 2, 3, 4, 5];
    arr.forEach((element) => console.log(element));
    // 输出: 1, 2, 3, 4, 5
  • for...of 循环:遍历数组的每个元素。

    const arr = [1, 2, 3, 4, 5];
    for (const element of arr) {
    console.log(element);
    }
    // 输出: 1, 2, 3, 4, 5

8. 字符串处理

  • toString():将数组转换为一个字符串,元素之间用逗号分隔。

    const arr = [1, 2, 3, 4, 5];
    const str = arr.toString(); // str -> "1,2,3,4,5"
  • toLocaleString():将数组转换为一个本地化的字符串,元素之间用逗号分隔。

    const arr = [1, 2, 3, 4, 5];
    const localStr = arr.toLocaleString(); // localStr -> "1,2,3,4,5"

9. 其他辅助方法

  • at(index):返回数组中指定索引的元素,支持负数索引。

    const arr = [1, 2, 3, 4, 5];
    const element = arr.at(-1); // element -> 5
  • flat(depth):将数组扁平化,深度可选,默认为 1。

    const arr = [1, [2, [3, [4, 5]]]];
    const flattened = arr.flat(2); // flattened -> [1, 2, 3, [4, 5]]
  • flatMap(callback, thisArg):先映射每个元素,然后展平结果数组。

    const arr = [1, 2, 3];
    const flatMapped = arr.flatMap((x) => [x, x * 2]); // flatMapped -> [1, 2, 2, 4, 3, 6]

10. 类型检测

  • isArray(value):检查传入的值是否为数组。
    const arr = [1, 2, 3];
    const isArr = Array.isArray(arr); // isArr -> true

11. 其他高级方法

  • from(arrayLike, mapFn, thisArg):从类数组对象或可迭代对象创建一个新的数组实例。

    const arrayLike = { 0: "a", 1: "b", length: 2 };
    const arr = Array.from(arrayLike); // arr -> ['a', 'b']
  • of(...args):根据一组值创建一个新的数组实例。

    const arr = Array.of(1, 2, 3); // arr -> [1, 2, 3]

12. 实验性方法

  • toReversed():返回一个新数组,其中元素顺序与原数组相反,原数组不变。

    const arr = [1, 2, 3];
    const reversed = arr.toReversed(); // reversed -> [3, 2, 1], arr -> [1, 2, 3]
  • toSorted(compareFunction):返回一个新数组,其中元素按指定顺序排序,原数组不变。

    const arr = [3, 1, 4, 1, 5];
    const sorted = arr.toSorted(); // sorted -> [1, 1, 3, 4, 5], arr -> [3, 1, 4, 1, 5]
  • toSpliced(start, deleteCount, ...items):返回一个新数组,其中在指定位置插入或删除了元素,原数组不变。

    const arr = [1, 2, 3, 4, 5];
    const spliced = arr.toSpliced(2, 1, 6); // spliced -> [1, 2, 6, 4, 5], arr -> [1, 2, 3, 4, 5]
  • with(index, value):返回一个新数组,其中指定索引的元素被替换,原数组不变。

    const arr = [1, 2, 3];
    const withNew = arr.with(1, 4); // withNew -> [1, 4, 3], arr -> [1, 2, 3]

总结

JavaScript 数组提供了丰富的内置方法,涵盖了从基本的添加、删除、查找、过滤、映射、排序到高级的类型检测和实验性方法。通过合理使用这些方法,可以极大地简化代码并提高开发效率。如果你有任何更具体的问题或需要进一步的解释,请告诉我!