zoukankan      html  css  js  c++  java
  • ES6(阮一峰) 数组的扩展

    1、扩展运算符

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

    1 console.log(1, ...[2, 3, 4], 5)
    2 // 1 2 3 4 5

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

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

    替代函数的apply方法

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

    扩展运算符的应用

    (1)复制数组

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

    (2)合并数组

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

    (3)与解构赋值结合

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

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

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

    (4)字符串

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

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

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

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

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

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

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

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

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

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

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



    2、Array.from()

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

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

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


    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


    4、数组实例的 copyWithin()

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

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


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

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

    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对象。

    1 function f(v){
    2   return v > this.age;
    3 }
    4 let person = {name: 'John', age: 20};
    5 [10, 12, 26, 15].find(f, person);    // 26

    上面的代码中,find函数接收了第二个参数person对象,回调函数中的this对象指向person对象。



    6、数组实例的 fill() 

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

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

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

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



    7、数组实例的 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 
     7 for (let elem of ['a', 'b'].values()) {
     8   console.log(elem);
     9 }
    10 // 'a'
    11 // 'b'
    12 
    13 for (let [index, elem] of ['a', 'b'].entries()) {
    14   console.log(index, elem);
    15 }
    16 // 0 "a"
    17 // 1 "b"


    8、数组的includes()

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

    该方法的第二个参数表示搜索的起始位置,默认为0

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


    9、数组的空位

    数组的空位指,数组的某一个位置没有任何值。

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

    ES6 则是明确将空位转为undefined

  • 相关阅读:
    es6 Set 和Map 数据结构
    es6 Symbol
    es6 对象的扩展
    es6 class
    es6 数组扩展方法
    Docker入门01——Image
    GORM 中文文档
    将以前的文章开始慢慢转到这里发表
    环境变量
    在 Linux 中安装 VMware Tools
  • 原文地址:https://www.cnblogs.com/1032473245jing/p/9109539.html
Copyright © 2011-2022 走看看