方法/属性 | 参数说明 | 描述 | 示例代码 |
---|---|---|---|
Array.isArray(array) | array (任意) | 判断一个值是否是数组 | Array.isArray([1, 2, 3]); // true |
array.length | 无 | 获取或设置数组的长度 | const arr = [1, 2, 3]; arr.length; // 3 |
array[index] | index (整数) | 访问或设置数组的元素 | const arr = [1, 2, 3]; arr[0]; // 1 |
array.at(index) | index :索引值,可以为负数表示从末尾开始计算的索引。 | 接受整数值并返回数组中该索引处的项目,允许正向和反向索引。 | [1, 2, 3].at(-1) // 3 |
方法 | 参数说明 | 描述 | 示例代码 |
---|---|---|---|
array.push(element1, ..., elementN) | element1, ..., elementN (任意) | 向数组的末尾添加一个或多个元素 | const arr = [1, 2]; arr.push(3, 4); // [1, 2, 3, 4] |
array.pop() | 无 | 移除数组的最后一个元素并返回该元素 | const arr = [1, 2, 3]; arr.pop(); // 3 |
array.unshift(element1, ..., elementN) | element1, ..., elementN (任意) | 向数组的开头添加一个或多个元素 | const arr = [1, 2]; arr.unshift(0); // [0, 1, 2] |
array.shift() | 无 | 移除数组的第一个元素并返回该元素 | const arr = [1, 2, 3]; arr.shift(); // 1 |
array.splice(start, deleteCount, item1, ..., itemN) | start (整数), deleteCount (整数, 可选), item1, ..., itemN (任意, 可选) | 删除或替换现有元素以及/或者添加新元素 | const arr = [1, 2, 3]; arr.splice(1, 1); // [1, 3] |
array.concat(array1, ..., arrayN) | array1, ..., arrayN (数组) | 合并两个或多个数组 | const arr1 = [1, 2]; const arr2 = [3, 4]; const arr3 = arr1.concat(arr2); // [1, 2, 3, 4] |
方法 | 参数说明 | 描述 | 示例代码 |
---|---|---|---|
array.indexOf(searchElement, fromIndex) | searchElement (任意), fromIndex (整数, 可选) | 返回数组中第一次出现指定元素的位置 | const arr = [1, 2, 3, 2]; arr.indexOf(2); // 1 |
array.lastIndexOf(searchElement, fromIndex) | searchElement (任意), fromIndex (整数, 可选) | 返回数组中最后一次出现指定元素的位置 | const arr = [1, 2, 3, 2]; arr.lastIndexOf(2); // 3 |
array.includes(searchElement, fromIndex) | searchElement (任意), fromIndex (整数, 可选) | 判断数组是否包含指定的元素 | const arr = [1, 2, 3]; arr.includes(2); // true |
array.find(callback, thisArg) | callback (函数), thisArg (任意, 可选) | 返回数组中满足提供的测试函数的第一个元素 | const arr = [1, 2, 3]; arr.find(element => element > 2); // 3 |
array.findIndex(callback, thisArg) | callback (函数), thisArg (任意, 可选) | 返回数组中满足提供的测试函数的第一个元素的索引 | const arr = [1, 2, 3]; arr.findIndex(element => element > 2); // 2 |
array.filter(callback, thisArg) | callback (函数), thisArg (任意, 可选) | 创建一个新数组,其中包含所有通过测试的元素 | const arr = [1, 2, 3]; arr.filter(element => element > 1); // [2, 3] |
方法 | 参数说明 | 描述 | 示例代码 |
---|---|---|---|
array.forEach(callback, thisArg) | callback (函数), thisArg (任意, 可选) | 为数组中的每个元素执行一次提供的函数 | const arr = [1, 2, 3]; arr.forEach(element => console.log(element)); |
array.map(callback, thisArg) | callback (函数), thisArg (任意, 可选) | 创建一个新数组,其结果是该数组中的每个元素调用一个提供的函数后的返回值 | const arr = [1, 2, 3]; const newArr = arr.map(element => element * 2); // [2, 4, 6] |
array.reduce(callback, initialValue) | callback (函数), initialValue (任意, 可选) | 对数组中的每个元素执行一个提供的函数,并将其结果汇总为单个返回值 | const arr = [1, 2, 3]; const sum = arr.reduce((acc, curr) => acc + curr, 0); // 6 |
array.reduceRight(callback, initialValue) | callback (函数), initialValue (任意, 可选) | 从右到左对数组中的每个元素执行一个提供的函数,并将其结果汇总为单个返回值 | const arr = [1, 2, 3]; const sum = arr.reduceRight((acc, curr) => acc + curr, 0); // 6 |
方法 | 参数说明 | 描述 | 示例代码 |
---|---|---|---|
array.slice(start, end) | start (整数, 可选), end (整数, 可选) | 返回一个新的数组对象,这一对象是一个从原始数组中提取出来的子数组 | const arr = [1, 2, 3, 4]; const newArr = arr.slice(1, 3); // [2, 3] |
array.sort(compareFunction) | compareFunction (函数, 可选) | 对数组的元素进行排序 | const arr = [3, 1, 2]; arr.sort(); // [1, 2, 3] |
array.reverse() | 无 | 反转数组中元素的顺序 | const arr = [1, 2, 3]; arr.reverse(); // [3, 2, 1] |
array.join(separator) | separator (字符串, 可选) | 将数组的所有元素连接成一个字符串 | const arr = [1, 2, 3]; const str = arr.join('-'); // '1-2-3' |
array.toString() | 无 | 返回一个字符串,表示指定的数组及其元素 | const arr = [1, 2, 3]; const str = arr.toString(); // '1,2,3' |
array.fill(value, start, end) | value (任意), start (整数, 可选), end (整数, 可选) | 用一个固定值填充数组中从起始索引到终止索引内的全部元素 | const arr = [1, 2, 3]; arr.fill(0, 1, 2); // [1, 0, 3] |
array.copyWithin(target, start, end) | target (整数), start (整数, 可选), end (整数, 可选) | 浅复制数组的一部分到同一数组中的另一个位置,并返回它,不会改变数组的长度 | const arr = [1, 2, 3, 4]; arr.copyWithin(0, 2, 4); // [3, 4, 3, 4] |
array.some(callback, thisArg) | callback (函数), thisArg (任意, 可选) | 测试数组中的某些元素是否通过由提供的函数实现的测试 | const arr = [1, 2, 3]; const hasEven = arr.some(element => element % 2 === 0); // true |
array.every(callback, thisArg) | callback (函数), thisArg (任意, 可选) | 测试数组中的所有元素是否通过由提供的函数实现的测试 | const arr = [1, 2, 3]; const allEven = arr.every(element => element % 2 === 0); // false |
array.flat(depth) | depth :指定要提取嵌套数组的结构深度,默认值为 1。 | 按照指定深度递归地将所有子数组连接,并返回一个新数组。 | [1, 2, [3, 4, [5, 6]]].flat(2) // [1, 2, 3, 4, 5, 6] |
array.flatMap(callback,thisArg) | callback :对每个元素执行的函数,thisArg :执行回调时用于 this 的值。 | 首先使用映射函数映射每个元素,然后将结果压缩成一个新数组。 | [1, 2, 3].flatMap(x => [x, x * 2]) // [1, 2, 2, 4, 3, 6] |
array.from(arrayLike, mapFn) | arrayLike :类似数组的对象或可迭代对象,mapFn :对每个元素执行的函数。 | 从类似数组或可迭代对象创建一个新的,浅拷贝的数组实例。 | Array.from('foo') // ['f', 'o', 'o'] |
array.of(elementN) | elementN :要创建数组的元素。 | 创建一个具有可变数量参数的新数组实例,而不考虑参数的数量或类型。 | Array.of(1, 2, 3) // [1, 2, 3] |