JavaScript 数组是动态、可修改的对象,用于存储有序的数据集合。以下是主要的数组方法,按功能分类列出:
一、 添加/删除元素 (修改原数组)
-
push(element1, ..., elementN): 在末尾添加一个或多个元素,返回新长度。let arr = [1, 2]; console.log(arr.push(3, 4)); // 输出: 4 console.log(arr); // 输出: [1, 2, 3, 4] -
pop(): 移除并返回最后一个元素。console.log(arr.pop()); // 输出: 4 console.log(arr); // 输出: [1, 2, 3] -
unshift(element1, ..., elementN): 在开头添加一个或多个元素,返回新长度。console.log(arr.unshift(0)); // 输出: 4 console.log(arr); // 输出: [0, 1, 2, 3] -
shift(): 移除并返回第一个元素。console.log(arr.shift()); // 输出: 0 console.log(arr); // 输出: [1, 2, 3] -
splice(start, deleteCount, item1, ..., itemN): 在指定位置删除/替换/添加元素,返回被删除的元素数组。// 删除:从索引1开始删除1个元素 console.log(arr.splice(1, 1)); // 输出: [2] console.log(arr); // 输出: [1, 3] // 替换:从索引1开始删除0个元素,然后添加'x','y' arr.splice(1, 0, 'x', 'y'); console.log(arr); // 输出: [1, 'x', 'y', 3]
二、 排序和反转 (修改原数组)
-
reverse(): 反转数组顺序。console.log(arr.reverse()); // 输出: [3, 'y', 'x', 1] console.log(arr); // 输出: [3, 'y', 'x', 1] -
sort(compareFunction): 对数组元素进行排序。默认按字符串Unicode码点排序。let nums = [3, 1, 4, 2]; nums.sort(); // 默认排序 console.log(nums); // 输出: [1, 2, 3, 4] nums.sort((a, b) => b - a); // 降序排列 console.log(nums); // 输出: [4, 3, 2, 1]
三、 合并、转换和填充 (通常不修改原数组,除了fill)
-
concat(array2, array3, ..., elementN): 合并数组或值,返回新数组。console.log([1, 2].concat([3, 4], 5)); // 输出: [1, 2, 3, 4, 5] -
join(separator): 将数组所有元素连接成一个字符串,用分隔符连接。console.log(['a', 'b', 'c'].join('-')); // 输出: "a-b-c" -
toString(): 返回由数组元素字符串形式连接而成的字符串(逗号分隔)。console.log([1, 2, 3].toString()); // 输出: "1,2,3" -
fill(value, start, end): 用固定值填充数组中从起始索引到结束索引内的全部元素。let fillArr = new Array(3); console.log(fillArr.fill(7)); // 输出: [7, 7, 7]
四、 查找和判断
-
indexOf(searchElement, fromIndex): 返回指定元素首次出现的索引,未找到返回-1。let arr = ['a', 'b', 'c', 'b']; console.log(arr.indexOf('b')); // 输出: 1 console.log(arr.indexOf('z')); // 输出: -1 -
lastIndexOf(searchElement, fromIndex): 返回指定元素最后出现的索引,未找到返回-1。console.log(arr.lastIndexOf('b')); // 输出: 3 -
includes(valueToFind, fromIndex): 判断数组是否包含某个值,返回布尔值。console.log(arr.includes('c')); // 输出: true console.log(arr.includes('c', 3)); // 输出: false (从索引3开始查找) -
find(callback): 返回满足测试函数的第一个元素的值,否则返回undefined。let nums = [5, 12, 8, 130, 44]; let found = nums.find(num => num > 10); console.log(found); // 输出: 12 -
findIndex(callback): 返回满足测试函数的第一个元素的索引,否则返回-1。console.log(nums.findIndex(num => num > 10)); // 输出: 1 -
findLast(callback) /findLastIndex(callback): (ES2023) 从数组末尾开始查找。console.log(nums.findLast(num => num > 10)); // 输出: 44 console.log(nums.findLastIndex(num => num > 10)); // 输出: 4
五、 遍历和迭代
-
forEach(callback): 对数组的每个元素执行一次提供的函数。[1, 2, 3].forEach(item => console.log(item * 2)); // 依次输出: 2, 4, 6 -
map(callback): 创建一个新数组,其结果是该数组中每个元素调用一次提供的函数后的返回值。let doubled = [1, 2, 3].map(item => item * 2); console.log(doubled); // 输出: [2, 4, 6] -
filter(callback): 创建一个新数组,包含所有通过测试函数的元素。let evens = [1, 2, 3, 4].filter(item => item % 2 === 0); console.log(evens); // 输出: [2, 4] -
reduce(callback, initialValue): 对数组元素从左到右执行一个reducer函数,将其结果汇总为单个返回值。let sum = [1, 2, 3, 4].reduce((acc, cur) => acc + cur, 0); console.log(sum); // 输出: 10 -
reduceRight(callback, initialValue): 与reduce相同,但从右到左执行。 -
some(callback): 测试数组中是否至少有一个元素通过测试函数,返回布尔值。console.log([1, 2, 3].some(x => x > 2)); // 输出: true -
every(callback): 测试数组中的所有元素是否都通过测试函数,返回布尔值。console.log([1, 2, 3].every(x => x > 0)); // 输出: true
六、 切片和连接 (不修改原数组)
-
slice(start, end): 返回从start到end(不包括)切片的新数组。let arr = [1, 2, 3, 4, 5]; console.log(arr.slice(1, 4)); // 输出: [2, 3, 4] console.log(arr); // 原数组不变: [1, 2, 3, 4, 5]
七、 数组静态方法
-
Array.isArray(value): 判断给定值是否为数组。console.log(Array.isArray([1, 2])); // 输出: true console.log(Array.isArray('string')); // 输出: false -
Array.from(arrayLike, mapFn): 从类数组对象或可迭代对象创建一个新的数组实例。console.log(Array.from('foo')); // 输出: ['f', 'o', 'o'] console.log(Array.from([1, 2, 3], x => x + x)); // 输出: [2, 4, 6] -
Array.of(element0, element1, ..., elementN): 用参数创建一个新数组实例。console.log(Array.of(1, 2, 3)); // 输出: [1, 2, 3] console.log(Array.of(7)); // 输出: [7] (与 new Array(7) 不同,后者创建长度为7的空数组)
八、 其他迭代和转换方法
-
flat(depth): 按照指定深度递归遍历数组,并将所有元素与遍历到的子数组中的元素合并为一个新数组返回。let nested = [1, [2, [3, [4]]]]; console.log(nested.flat(2)); // 输出: [1, 2, 3, [4]] -
flatMap(callback): 先对数组每个元素执行映射函数,然后将结果扁平化一级,返回新数组。console.log([1, 2, 3].flatMap(x => [x, x * 2])); // 输出: [1, 2, 2, 4, 3, 6] -
keys(),values(),entries(): 返回数组索引、值、键值对的迭代器。for (let key of ['a', 'b'].keys()) console.log(key); // 输出: 0, 1 for (let value of ['a', 'b'].values()) console.log(value); // 输出: 'a', 'b' for (let [index, value] of ['a', 'b'].entries()) console.log(index, value); // 输出: 0 'a', 1 'b'
核心特性提醒:数组方法分为改变原数组(如 push, pop, splice, sort, reverse, fill)和不改变原数组(如 slice, concat, map, filter)两类。在使用时需要注意区别,以避免意外的副作用。