• 数组的扩展
    • 扩展运算符
      • 含义
      • 替代数组的 apply 方法
      • 扩展运算符的应用
    • Array.from()
    • Array.of()
    • 数组实例的 copyWithin()
    • 数组实例的 find() 和 findIndex()
    • 数组实例的 fill()
    • 数组实例的 entries(),keys() 和 values()
    • 数组实例的 includes()
    • 数组的空位

    数组的扩展

    扩展运算符

    含义

    扩展运算符(spread)是三个点(...)。它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列。

    1. console.log(...[1, 2, 3])
    2. // 1 2 3
    3. console.log(1, ...[2, 3, 4], 5)
    4. // 1 2 3 4 5
    5. [...document.querySelectorAll('div')]
    6. // [<div>, <div>, <div>]

    该运算符主要用于函数调用。

    1. function push(array, ...items) {
    2. array.push(...items);
    3. }
    4. function add(x, y) {
    5. return x + y;
    6. }
    7. const numbers = [4, 38];
    8. add(...numbers) // 42

    上面代码中,array.push(...items)add(...numbers)这两行,都是函数的调用,它们的都使用了扩展运算符。该运算符将一个数组,变为参数序列。

    扩展运算符与正常的函数参数可以结合使用,非常灵活。

    1. function f(v, w, x, y, z) { }
    2. const args = [0, 1];
    3. f(-1, ...args, 2, ...[3]);

    扩展运算符后面还可以放置表达式。

    1. const arr = [
    2. ...(x > 0 ? ['a'] : []),
    3. 'b',
    4. ];

    如果扩展运算符后面是一个空数组,则不产生任何效果。

    1. [...[], 1]
    2. // [1]

    替代数组的 apply 方法

    由于扩展运算符可以展开数组,所以不再需要apply方法,将数组转为函数的参数了。

    1. // ES5 的写法
    2. function f(x, y, z) {
    3. // ...
    4. }
    5. var args = [0, 1, 2];
    6. f.apply(null, args);
    7. // ES6的写法
    8. function f(x, y, z) {
    9. // ...
    10. }
    11. let args = [0, 1, 2];
    12. f(...args);

    下面是扩展运算符取代apply方法的一个实际的例子,应用Math.max方法,简化求出一个数组最大元素的写法。

    1. // ES5 的写法
    2. Math.max.apply(null, [14, 3, 77])
    3. // ES6 的写法
    4. Math.max(...[14, 3, 77])
    5. // 等同于
    6. Math.max(14, 3, 77);

    上面代码中,由于 JavaScript 不提供求数组最大元素的函数,所以只能套用Math.max函数,将数组转为一个参数序列,然后求最大值。有了扩展运算符以后,就可以直接用Math.max了。

    另一个例子是通过push函数,将一个数组添加到另一个数组的尾部。

    1. // ES5的 写法
    2. var arr1 = [0, 1, 2];
    3. var arr2 = [3, 4, 5];
    4. Array.prototype.push.apply(arr1, arr2);
    5. // ES6 的写法
    6. let arr1 = [0, 1, 2];
    7. let arr2 = [3, 4, 5];
    8. arr1.push(...arr2);

    上面代码的 ES5 写法中,push方法的参数不能是数组,所以只好通过apply方法变通使用push方法。有了扩展运算符,就可以直接将数组传入push方法。

    下面是另外一个例子。

    1. // ES5
    2. new (Date.bind.apply(Date, [null, 2015, 1, 1]))
    3. // ES6
    4. new Date(...[2015, 1, 1]);

    扩展运算符的应用

    (1)复制数组

    数组是复合的数据类型,直接复制的话,只是复制了指向底层数据结构的指针,而不是克隆一个全新的数组。

    1. const a1 = [1, 2];
    2. const a2 = a1;
    3. a2[0] = 2;
    4. a1 // [2, 2]

    上面代码中,a2并不是a1的克隆,而是指向同一份数据的另一个指针。修改a2,会直接导致a1的变化。

    ES5 只能用变通方法来复制数组。

    1. const a1 = [1, 2];
    2. const a2 = a1.concat();
    3. a2[0] = 2;
    4. a1 // [1, 2]

    上面代码中,a1会返回原数组的克隆,再修改a2就不会对a1产生影响。

    扩展运算符提供了复制数组的简便写法。

    1. const a1 = [1, 2];
    2. // 写法一
    3. const a2 = [...a1];
    4. // 写法二
    5. const [...a2] = a1;

    上面的两种写法,a2都是a1的克隆。

    (2)合并数组

    扩展运算符提供了数组合并的新写法。

    1. // ES5
    2. [1, 2].concat(more)
    3. // ES6
    4. [1, 2, ...more]
    5. var arr1 = ['a', 'b'];
    6. var arr2 = ['c'];
    7. var arr3 = ['d', 'e'];
    8. // ES5的合并数组
    9. arr1.concat(arr2, arr3);
    10. // [ 'a', 'b', 'c', 'd', 'e' ]
    11. // ES6的合并数组
    12. [...arr1, ...arr2, ...arr3]
    13. // [ 'a', 'b', 'c', 'd', 'e' ]

    (3)与解构赋值结合

    扩展运算符可以与解构赋值结合起来,用于生成数组。

    1. // ES5
    2. a = list[0], rest = list.slice(1)
    3. // ES6
    4. [a, ...rest] = list

    下面是另外一些例子。

    1. const [first, ...rest] = [1, 2, 3, 4, 5];
    2. first // 1
    3. rest // [2, 3, 4, 5]
    4. const [first, ...rest] = [];
    5. first // undefined
    6. rest // []
    7. const [first, ...rest] = ["foo"];
    8. first // "foo"
    9. rest // []

    如果将扩展运算符用于数组赋值,只能放在参数的最后一位,否则会报错。

    1. const [...butLast, last] = [1, 2, 3, 4, 5];
    2. // 报错
    3. const [first, ...middle, last] = [1, 2, 3, 4, 5];
    4. // 报错

    (4)字符串

    扩展运算符还可以将字符串转为真正的数组。

    1. [...'hello']
    2. // [ "h", "e", "l", "l", "o" ]

    上面的写法,有一个重要的好处,那就是能够正确识别四个字节的 Unicode 字符。

    1. 'x\uD83D\uDE80y'.length // 4
    2. [...'x\uD83D\uDE80y'].length // 3

    上面代码的第一种写法,JavaScript 会将四个字节的 Unicode 字符,识别为 2 个字符,采用扩展运算符就没有这个问题。因此,正确返回字符串长度的函数,可以像下面这样写。

    1. function length(str) {
    2. return [...str].length;
    3. }
    4. length('x\uD83D\uDE80y') // 3

    凡是涉及到操作四个字节的 Unicode 字符的函数,都有这个问题。因此,最好都用扩展运算符改写。

    1. let str = 'x\uD83D\uDE80y';
    2. str.split('').reverse().join('')
    3. // 'y\uDE80\uD83Dx'
    4. [...str].reverse().join('')
    5. // 'y\uD83D\uDE80x'

    上面代码中,如果不用扩展运算符,字符串的reverse操作就不正确。

    (5)实现了 Iterator 接口的对象

    任何 Iterator 接口的对象(参阅 Iterator 一章),都可以用扩展运算符转为真正的数组。

    1. let nodeList = document.querySelectorAll('div');
    2. let array = [...nodeList];

    上面代码中,querySelectorAll方法返回的是一个nodeList对象。它不是数组,而是一个类似数组的对象。这时,扩展运算符可以将其转为真正的数组,原因就在于NodeList对象实现了 Iterator 。

    对于那些没有部署 Iterator 接口的类似数组的对象,扩展运算符就无法将其转为真正的数组。

    1. let arrayLike = {
    2. '0': 'a',
    3. '1': 'b',
    4. '2': 'c',
    5. length: 3
    6. };
    7. // TypeError: Cannot spread non-iterable object.
    8. let arr = [...arrayLike];

    上面代码中,arrayLike是一个类似数组的对象,但是没有部署 Iterator 接口,扩展运算符就会报错。这时,可以改为使用Array.from方法将arrayLike转为真正的数组。

    (6)Map 和 Set 结构,Generator 函数

    扩展运算符内部调用的是数据结构的 Iterator 接口,因此只要具有 Iterator 接口的对象,都可以使用扩展运算符,比如 Map 结构。

    1. let map = new Map([
    2. [1, 'one'],
    3. [2, 'two'],
    4. [3, 'three'],
    5. ]);
    6. let arr = [...map.keys()]; // [1, 2, 3]

    Generator 函数运行后,返回一个遍历器对象,因此也可以使用扩展运算符。

    1. const go = function*(){
    2. yield 1;
    3. yield 2;
    4. yield 3;
    5. };
    6. [...go()] // [1, 2, 3]

    上面代码中,变量go是一个 Generator 函数,执行后返回的是一个遍历器对象,对这个遍历器对象执行扩展运算符,就会将内部遍历得到的值,转为一个数组。

    如果对没有 Iterator 接口的对象,使用扩展运算符,将会报错。

    1. const obj = {a: 1, b: 2};
    2. let arr = [...obj]; // TypeError: Cannot spread non-iterable object

    Array.from()

    Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括 ES6 新增的数据结构 Set 和 Map)。

    下面是一个类似数组的对象,Array.from将它转为真正的数组。

    1. let arrayLike = {
    2. '0': 'a',
    3. '1': 'b',
    4. '2': 'c',
    5. length: 3
    6. };
    7. // ES5的写法
    8. var arr1 = [].slice.call(arrayLike); // ['a', 'b', 'c']
    9. // ES6的写法
    10. let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']

    实际应用中,常见的类似数组的对象是 DOM 操作返回的 NodeList 集合,以及函数内部的arguments对象。Array.from都可以将它们转为真正的数组。

    1. // NodeList对象
    2. let ps = document.querySelectorAll('p');
    3. Array.from(ps).forEach(function (p) {
    4. console.log(p);
    5. });
    6. // arguments对象
    7. function foo() {
    8. var args = Array.from(arguments);
    9. // ...
    10. }

    上面代码中,querySelectorAll方法返回的是一个类似数组的对象,可以将这个对象转为真正的数组,再使用forEach方法。

    只要是部署了 Iterator 接口的数据结构,Array.from都能将其转为数组。

    1. Array.from('hello')
    2. // ['h', 'e', 'l', 'l', 'o']
    3. let namesSet = new Set(['a', 'b'])
    4. Array.from(namesSet) // ['a', 'b']

    上面代码中,字符串和 Set 结构都具有 Iterator 接口,因此可以被Array.from转为真正的数组。

    如果参数是一个真正的数组,Array.from会返回一个一模一样的新数组。

    1. Array.from([1, 2, 3])
    2. // [1, 2, 3]

    值得提醒的是,扩展运算符(...)也可以将某些数据结构转为数组。

    1. // arguments对象
    2. function foo() {
    3. const args = [...arguments];
    4. }
    5. // NodeList对象
    6. [...document.querySelectorAll('div')]

    扩展运算符背后调用的是遍历器接口(Symbol.iterator),如果一个对象没有部署这个接口,就无法转换。Array.from方法还支持类似数组的对象。所谓类似数组的对象,本质特征只有一点,即必须有length属性。因此,任何有length属性的对象,都可以通过Array.from方法转为数组,而此时扩展运算符就无法转换。

    1. Array.from({ length: 3 });
    2. // [ undefined, undefined, undefined ]

    上面代码中,Array.from返回了一个具有三个成员的数组,每个位置的值都是undefined。扩展运算符转换不了这个对象。

    对于还没有部署该方法的浏览器,可以用Array.prototype.slice方法替代。

    1. const toArray = (() =>
    2. Array.from ? Array.from : obj => [].slice.call(obj)
    3. )();

    Array.from还可以接受第二个参数,作用类似于数组的map方法,用来对每个元素进行处理,将处理后的值放入返回的数组。

    1. Array.from(arrayLike, x => x * x);
    2. // 等同于
    3. Array.from(arrayLike).map(x => x * x);
    4. Array.from([1, 2, 3], (x) => x * x)
    5. // [1, 4, 9]

    下面的例子是取出一组 DOM 节点的文本内容。

    1. let spans = document.querySelectorAll('span.name');
    2. // map()
    3. let names1 = Array.prototype.map.call(spans, s => s.textContent);
    4. // Array.from()
    5. let names2 = Array.from(spans, s => s.textContent)

    下面的例子将数组中布尔值为false的成员转为0

    1. Array.from([1, , 2, , 3], (n) => n || 0)
    2. // [1, 0, 2, 0, 3]

    另一个例子是返回各种数据的类型。

    1. function typesOf () {
    2. return Array.from(arguments, value => typeof value)
    3. }
    4. typesOf(null, [], NaN)
    5. // ['object', 'object', 'number']

    如果map函数里面用到了this关键字,还可以传入Array.from的第三个参数,用来绑定this

    Array.from()可以将各种值转为真正的数组,并且还提供map功能。这实际上意味着,只要有一个原始的数据结构,你就可以先对它的值进行处理,然后转成规范的数组结构,进而就可以使用数量众多的数组方法。

    1. Array.from({ length: 2 }, () => 'jack')
    2. // ['jack', 'jack']

    上面代码中,Array.from的第一个参数指定了第二个参数运行的次数。这种特性可以让该方法的用法变得非常灵活。

    Array.from()的另一个应用是,将字符串转为数组,然后返回字符串的长度。因为它能正确处理各种 Unicode 字符,可以避免 JavaScript 将大于\uFFFF的 Unicode 字符,算作两个字符的 bug。

    1. function countSymbols(string) {
    2. return Array.from(string).length;
    3. }

    Array.of()

    Array.of方法用于将一组值,转换为数组。

    1. Array.of(3, 11, 8) // [3,11,8]
    2. Array.of(3) // [3]
    3. Array.of(3).length // 1

    这个方法的主要目的,是弥补数组构造函数Array()的不足。因为参数个数的不同,会导致Array()的行为有差异。

    1. Array() // []
    2. Array(3) // [, , ,]
    3. Array(3, 11, 8) // [3, 11, 8]

    上面代码中,Array方法没有参数、一个参数、三个参数时,返回结果都不一样。只有当参数个数不少于 2 个时,Array()才会返回由参数组成的新数组。参数个数只有一个时,实际上是指定数组的长度。

    Array.of基本上可以用来替代Array()new Array(),并且不存在由于参数不同而导致的重载。它的行为非常统一。

    1. Array.of() // []
    2. Array.of(undefined) // [undefined]
    3. Array.of(1) // [1]
    4. Array.of(1, 2) // [1, 2]

    Array.of总是返回参数值组成的数组。如果没有参数,就返回一个空数组。

    Array.of方法可以用下面的代码模拟实现。

    1. function ArrayOf(){
    2. return [].slice.call(arguments);
    3. }

    数组实例的 copyWithin()

    数组实例的copyWithin方法,在当前数组内部,将指定位置的成员复制到其他位置(会覆盖原有成员),然后返回当前数组。也就是说,使用这个方法,会修改当前数组。

    1. Array.prototype.copyWithin(target, start = 0, end = this.length)

    它接受三个参数。

    • target(必需):从该位置开始替换数据。
    • start(可选):从该位置开始读取数据,默认为 0。如果为负值,表示倒数。
    • end(可选):到该位置前停止读取数据,默认等于数组长度。如果为负值,表示倒数。

    这三个参数都应该是数值,如果不是,会自动转为数值。

    1. [1, 2, 3, 4, 5].copyWithin(0, 3)
    2. // [4, 5, 3, 4, 5]

    上面代码表示将从 3 号位直到数组结束的成员(4 和 5),复制到从 0 号位开始的位置,结果覆盖了原来的 1 和 2。

    下面是更多例子。

    1. // 将3号位复制到0号位
    2. [1, 2, 3, 4, 5].copyWithin(0, 3, 4)
    3. // [4, 2, 3, 4, 5]
    4. // -2相当于3号位,-1相当于4号位
    5. [1, 2, 3, 4, 5].copyWithin(0, -2, -1)
    6. // [4, 2, 3, 4, 5]
    7. // 将3号位复制到0号位
    8. [].copyWithin.call({length: 5, 3: 1}, 0, 3)
    9. // {0: 1, 3: 1, length: 5}
    10. // 将2号位到数组结束,复制到0号位
    11. let i32a = new Int32Array([1, 2, 3, 4, 5]);
    12. i32a.copyWithin(0, 2);
    13. // Int32Array [3, 4, 5, 4, 5]
    14. // 对于没有部署 TypedArray 的 copyWithin 方法的平台
    15. // 需要采用下面的写法
    16. [].copyWithin.call(new Int32Array([1, 2, 3, 4, 5]), 0, 3, 4);
    17. // Int32Array [4, 2, 3, 4, 5]

    数组实例的 find() 和 findIndex()

    数组实例的find方法,用于找出第一个符合条件的数组成员。它的参数是一个回调函数,所有数组成员依次执行该回调函数,直到找出第一个返回值为true的成员,然后返回该成员。如果没有符合条件的成员,则返回undefined

    1. [1, 4, -5, 10].find((n) => n < 0)
    2. // -5

    上面代码找出数组中第一个小于 0 的成员。

    1. [1, 5, 10, 15].find(function(value, index, arr) {
    2. return value > 9;
    3. }) // 10

    上面代码中,find方法的回调函数可以接受三个参数,依次为当前的值、当前的位置和原数组。

    数组实例的findIndex方法的用法与find方法非常类似,返回第一个符合条件的数组成员的位置,如果所有成员都不符合条件,则返回-1

    1. [1, 5, 10, 15].findIndex(function(value, index, arr) {
    2. return value > 9;
    3. }) // 2

    这两个方法都可以接受第二个参数,用来绑定回调函数的this对象。

    另外,这两个方法都可以发现NaN,弥补了数组的indexOf方法的不足。

    1. [NaN].indexOf(NaN)
    2. // -1
    3. [NaN].findIndex(y => Object.is(NaN, y))
    4. // 0

    上面代码中,indexOf方法无法识别数组的NaN成员,但是findIndex方法可以借助Object.is方法做到。

    数组实例的 fill()

    fill方法使用给定值,填充一个数组。

    1. ['a', 'b', 'c'].fill(7)
    2. // [7, 7, 7]
    3. new Array(3).fill(7)
    4. // [7, 7, 7]

    上面代码表明,fill方法用于空数组的初始化非常方便。数组中已有的元素,会被全部抹去。

    fill方法还可以接受第二个和第三个参数,用于指定填充的起始位置和结束位置。

    1. ['a', 'b', 'c'].fill(7, 1, 2)
    2. // ['a', 7, 'c']

    上面代码表示,fill方法从 1 号位开始,向原数组填充 7,到 2 号位之前结束。

    数组实例的 entries(),keys() 和 values()

    ES6 提供三个新的方法——entries()keys()values()——用于遍历数组。它们都返回一个遍历器对象(详见《Iterator》一章),可以用for...of循环进行遍历,唯一的区别是keys()是对键名的遍历、values()是对键值的遍历,entries()是对键值对的遍历。

    1. for (let index of ['a', 'b'].keys()) {
    2. console.log(index);
    3. }
    4. // 0
    5. // 1
    6. for (let elem of ['a', 'b'].values()) {
    7. console.log(elem);
    8. }
    9. // 'a'
    10. // 'b'
    11. for (let [index, elem] of ['a', 'b'].entries()) {
    12. console.log(index, elem);
    13. }
    14. // 0 "a"
    15. // 1 "b"

    如果不使用for...of循环,可以手动调用遍历器对象的next方法,进行遍历。

    1. let letter = ['a', 'b', 'c'];
    2. let entries = letter.entries();
    3. console.log(entries.next().value); // [0, 'a']
    4. console.log(entries.next().value); // [1, 'b']
    5. console.log(entries.next().value); // [2, 'c']

    数组实例的 includes()

    Array.prototype.includes方法返回一个布尔值,表示某个数组是否包含给定的值,与字符串的includes方法类似。ES2016 引入了该方法。

    1. [1, 2, 3].includes(2) // true
    2. [1, 2, 3].includes(4) // false
    3. [1, 2, NaN].includes(NaN) // true

    该方法的第二个参数表示搜索的起始位置,默认为0。如果第二个参数为负数,则表示倒数的位置,如果这时它大于数组长度(比如第二个参数为-4,但数组长度为3),则会重置为从0开始。

    1. [1, 2, 3].includes(3, 3); // false
    2. [1, 2, 3].includes(3, -1); // true

    没有该方法之前,我们通常使用数组的indexOf方法,检查是否包含某个值。

    1. if (arr.indexOf(el) !== -1) {
    2. // ...
    3. }

    indexOf方法有两个缺点,一是不够语义化,它的含义是找到参数值的第一个出现位置,所以要去比较是否不等于-1,表达起来不够直观。二是,它内部使用严格相等运算符(===)进行判断,这会导致对NaN的误判。

    1. [NaN].indexOf(NaN)
    2. // -1

    includes使用的是不一样的判断算法,就没有这个问题。

    1. [NaN].includes(NaN)
    2. // true

    下面代码用来检查当前环境是否支持该方法,如果不支持,部署一个简易的替代版本。

    1. const contains = (() =>
    2. Array.prototype.includes
    3. ? (arr, value) => arr.includes(value)
    4. : (arr, value) => arr.some(el => el === value)
    5. )();
    6. contains(['foo', 'bar'], 'baz'); // => false

    另外,Map 和 Set 数据结构有一个has方法,需要注意与includes区分。

    • Map 结构的has方法,是用来查找键名的,比如Map.prototype.has(key)WeakMap.prototype.has(key)Reflect.has(target, propertyKey)
    • Set 结构的has方法,是用来查找值的,比如Set.prototype.has(value)WeakSet.prototype.has(value)

    数组的空位

    数组的空位指,数组的某一个位置没有任何值。比如,Array构造函数返回的数组都是空位。

    1. Array(3) // [, , ,]

    上面代码中,Array(3)返回一个具有 3 个空位的数组。

    注意,空位不是undefined,一个位置的值等于undefined,依然是有值的。空位是没有任何值,in运算符可以说明这一点。

    1. 0 in [undefined, undefined, undefined] // true
    2. 0 in [, , ,] // false

    上面代码说明,第一个数组的 0 号位置是有值的,第二个数组的 0 号位置没有值。

    ES5 对空位的处理,已经很不一致了,大多数情况下会忽略空位。

    • forEach(), filter(), every()some()都会跳过空位。
    • map()会跳过空位,但会保留这个值
    • join()toString()会将空位视为undefined,而undefinednull会被处理成空字符串。
    1. // forEach方法
    2. [,'a'].forEach((x,i) => console.log(i)); // 1
    3. // filter方法
    4. ['a',,'b'].filter(x => true) // ['a','b']
    5. // every方法
    6. [,'a'].every(x => x==='a') // true
    7. // some方法
    8. [,'a'].some(x => x !== 'a') // false
    9. // map方法
    10. [,'a'].map(x => 1) // [,1]
    11. // join方法
    12. [,'a',undefined,null].join('#') // "#a##"
    13. // toString方法
    14. [,'a',undefined,null].toString() // ",a,,"

    ES6 则是明确将空位转为undefined

    Array.from方法会将数组的空位,转为undefined,也就是说,这个方法不会忽略空位。

    1. Array.from(['a',,'b'])
    2. // [ "a", undefined, "b" ]

    扩展运算符(...)也会将空位转为undefined

    1. [...['a',,'b']]
    2. // [ "a", undefined, "b" ]

    copyWithin()会连空位一起拷贝。

    1. [,'a','b',,].copyWithin(2,0) // [,"a",,"a"]

    fill()会将空位视为正常的数组位置。

    1. new Array(3).fill('a') // ["a","a","a"]

    for...of循环也会遍历空位。

    1. let arr = [, ,];
    2. for (let i of arr) {
    3. console.log(1);
    4. }
    5. // 1
    6. // 1

    上面代码中,数组arr有两个空位,for...of并没有忽略它们。如果改成map方法遍历,空位是会跳过的。

    entries()keys()values()find()findIndex()会将空位处理成undefined

    1. // entries()
    2. [...[,'a'].entries()] // [[0,undefined], [1,"a"]]
    3. // keys()
    4. [...[,'a'].keys()] // [0,1]
    5. // values()
    6. [...[,'a'].values()] // [undefined,"a"]
    7. // find()
    8. [,'a'].find(x => true) // undefined
    9. // findIndex()
    10. [,'a'].findIndex(x => true) // 0

    由于空位的处理规则非常不统一,所以建议避免出现空位。