Last updated on December 6, 2025 am
数组是 JavaScript 处理数据的核心结构,ES6(ECMAScript 2015)对数组的扩展,解决了传统操作的繁琐问题,让数据处理更高效。下面我们来结合实例,梳理 ES6 数组的核心扩展特性与用法。
一、语法层面的突破:扩展运算符
扩展运算符(...)是 ES6 中最具代表性的语法糖之一,它将数组或类数组对象展开为独立的元素,彻底改变了数组拷贝、合并、参数传递等常见操作的实现方式。
1. 数组拷贝
传统拷贝数组需借助slice()或concat(),ES6 中使用扩展运算符更为简洁直观,且支持深拷贝一维数组:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
const arr1 = [1, 2, 3];
const arr2 = [...arr1];
arr2[0] = 100;
console.log(arr1);
const arr3 = arr1.slice();
const arr4 = [].concat(arr1);
|
2. 数组合并
无需再依赖concat()的链式调用,扩展运算符可直接合并多个数组,且支持在任意位置插入元素:
1 2 3 4 5
| const arrA = [1, 2];
const arrB = [3, 4];
const arrC = [0, ...arrA, 2.5, ...arrB, 5];
|
3. 函数参数传递
解决了apply()传递数组参数的繁琐问题,直接将数组元素作为独立参数传入函数:
1 2 3 4 5 6 7 8 9
| const numbers = [10, 20, 30];
Math.max(...numbers);
Math.max.apply(null, numbers);
|
4. 解构赋值结合
扩展运算符可与解构赋值配合,便捷提取数组部分元素:
1 2 3 4 5 6 7
| const [first, second, ...rest] = [1, 2, 3, 4, 5];
console.log(first);
console.log(second);
console.log(rest);
|
二、数组构造函数的增强:Array.from () 与 Array.of ()
ES6 为Array构造函数新增了两个静态方法,分别解决了 “类数组对象转数组” 和 “创建固定长度数组” 的经典问题。
1. Array.from ():类数组对象与可迭代对象转数组
类数组对象(如 DOM 集合、arguments)和可迭代对象(如 Set、Map),在 ES6 前需通过Array.prototype.slice.call()转换为数组,Array.from()提供了统一、直观的解决方案,还支持映射函数参数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
const lis = document.querySelectorAll('li');
const liArray = Array.from(lis);
function sum() { return Array.from(arguments).reduce((a, b) => a + b, 0); }
sum(1, 2, 3);
const str = 'hello';
Array.from(str, char => char.toUpperCase());
const set = new Set([1, 2, 2, 3]);
Array.from(set);
|
2. Array.of ():统一数组创建行为
传统Array构造函数存在行为不一致问题:当传入单个数字时,创建的是指定长度的空数组,而非包含该数字的数组。Array.of()修复了这一缺陷,无论传入多少参数,均创建包含这些参数的数组:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
Array.of(5);
Array.of(1, 2, 3);
Array.of();
Array(5);
Array(1, 2, 3);
|
三、数组实例方法的升级:查找、遍历与修改
ES6 新增了多个数组实例方法,覆盖了查找、遍历、填充、扁平化等高频场景,让数据处理逻辑更简洁。
1. 查找类方法:find () 与 findIndex ()
传统查找数组元素依赖indexOf(),但它仅支持查找原始值,且无法处理 “满足特定条件的元素”。find()和findIndex()弥补了这一不足,支持传入回调函数,返回第一个满足条件的元素或其索引:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| const users = [ { id: 1, name: '张三', age: 20 }, { id: 2, name: '李四', age: 25 }, { id: 3, name: '王五', age: 30 } ];
const targetUser = users.find(user => user.age > 22);
console.log(targetUser);
const targetIndex = users.findIndex(user => user.name === '王五');
console.log(targetIndex);
const numbers = [1, NaN, 3];
numbers.find(n => Object.is(n, NaN));
numbers.findIndex(n => Object.is(n, NaN));
|
2. 包含判断:includes ()
indexOf()判断元素是否存在时,需通过返回值是否为-1判断,语义不够清晰。includes()直接返回布尔值,且支持判断NaN,使用更直观:
1 2 3 4 5 6 7 8 9 10 11
| const fruits = ['苹果', '香蕉', '橙子'];
fruits.includes('香蕉');
fruits.includes('葡萄');
[1, 2, NaN].includes(NaN);
[1, 2, NaN].indexOf(NaN);
|
3. 迭代器方法:entries ()、keys ()、values ()
ES6 为数组新增了三个迭代器方法,支持通过for...of循环遍历数组的键、值或键值对,替代了传统的for循环或forEach():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| const arr = ['a', 'b', 'c'];
for (const key of arr.keys()) {
  console.log(key);
}
for (const value of arr.values()) {
console.log(value);
}
for (const [key, value] of arr.entries()) {
console.log(`${key}: ${value}`);
}
|
4. 填充与复制:fill () 与 copyWithin ()
fill()用于将数组指定范围的元素替换为固定值,copyWithin()则将数组内部的元素复制到另一位置,均支持修改原数组(也可通过浅拷贝避免):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
const arr1 = [1, 2, 3, 4, 5];
arr1.fill(0);
arr1.fill(9, 1, 3);
const arr2 = [1, 2, 3, 4, 5];
arr2.copyWithin(0, 3);
arr2.copyWithin(1, 0, 2);
|
5. 扁平化处理:flat 与 flatMap
多维数组扁平化是常见需求,ES6 前需递归实现,flat()和flatMap()提供了原生支持:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
const nestedArr = [1, [2, [3, [4]]]];
nestedArr.flat();
nestedArr.flat(2);
nestedArr.flat(Infinity);
const arr = [1, 2, 3];
arr.flatMap(x => [x * 2]);
const lists = [ ['a', 'b'], ['c', 'd'], ['e'] ];
lists.flatMap(list => list.map(item => item.toUpperCase()));
|
四、空位处理的规范化
ES5 中数组空位(如[1, , 3])的行为不一致(forEach()、map()跳过空位,join()视为空字符串)。ES6 明确规定:数组空位应被视为undefined,新增方法(如find()、includes())均按undefined处理,避免了歧义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| const arr = [1, , 3];
arr.includes(undefined);
arr.find(x => x === undefined);
arr.flat();
arr.map(x => x || 0);
arr.join('-');
|
ES6中数组的拓展用简洁语法和实用方法,解决了传统操作的痛点,减少代码量、提升可读性。掌握这些特性,能帮助我们更高效地处理项目中的数据。
最关键的是,面试频率很高啊,哈哈!