ECMAScript6

270 阅读2分钟

ECMAScript6

数组

  1. 扩展运算符(...)

    1. 将一个数组/对象转化为参数队列

      let item = [1,2,3]
      console.log(this.array)  //第一个结果
      console.log(...this.array)  //第二个结果
      

image-20210707161116421.png 2. 其中的push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度;push和apply、add(a+b)相比,我觉得很好用

  ```
  let item = [1,2,3]
  console.log(...this.array)
  this.array.push(...item)
  console.log(...this.array)
  ```
 

image-20210707161727408.png

  1. 数组复制

    const a = [1,2];
    const a1 = [...a];  //如果是const a1 = a ;就是指向同一份数据的另一个指针,a会改变;但是[...a]不会导致原数组改变
    a1[0] = 2;
    console.log(...a)
    
  2. array.from、array.of 转为数组

          let array = {
            '0':'a',
            '1':'s',
            '2':'q',
            length:3
          }
          let arr1 = Array.from(array)
          console.log(array)
          console.log(...array)
          console.log(arr1)
    

    其中涉及set/map数据结构,详细见

    [www.cnblogs.com/houxianzhou…]:

    setmap
    一种叫做集合的数据结构,一种叫做字典的数据结构
    集合 是由一堆无序的、相关联的,且不重复的内存结构【数学中称为元素】组成的组合字典 是一些元素的集合。每个元素有一个称作key 的域,不同元素的key 各不相同
    const s = new Set();const m = new Map()
  3. copywith:数组内的指定位置复制粘贴

  4. find()/findIndex()

    1. find方法用于查找第一个符合条件的数组成员

      let person = {name:'John',age: 20};
      let a = [10,12,23,-5].find((n) => n <0);
      console.log(a);
      
    2. findIndex()用于查找第一个符合条件的数组成员的位置

  5. fill()

    let arr = new Array(3).fill(7)
    console.log(arr);  //输出[7,7,7]
    
  6. 遍历数组

    1. entries() 键值对

      let msg = [
              {num:'192',value:'www'},
              {num:'138',value:'qqq'},
              {num:'172',value:'yyy'}
            ]
            for (let index of msg.entries()) {
              console.log(index);       
            }
      

      输出结果是:

      (2) [0, {…}] (2) [1, {…}] (2) [2, {…}]

    2. keys():对键名的遍历

      let msg = [
              {num:'192',value:'www'},
              {num:'138',value:'qqq'},
              {num:'172',value:'yyy'}
            ]
            for (let index of msg.keys()) {
              console.log(index);       
            }
      //输出结果
      0
      1
      2
      
    3. values() 对键值的遍历

      let msg = [        {num:'192',value:'www'},        {num:'138',value:'qqq'},        {num:'172',value:'yyy'}      ]      for (let index of msg.values()) {        console.log(index);             }      //输出结果{num: "192", value: "www"}{num: "138", value: "qqq"}{num: "172", value: "yyy"}
      
  7. includes() 数组实例

    let msg = [        1,2,3      ]      let result = msg.includes(1)      console.log(result);     //输出为true
    
  8. flat() 拉平数组

    默认拉平层的参数为1

    参数为Infinity

    let msg = [        1,2,3,[3,[2,4,9]]      ]      let result = msg.flat(2)  //如果不加2,输出为(5) [1, 2, 3, 3, Array(3)]      console.log(result);      //结果为(7) [1, 2, 3, 3, 2, 4, 9]      //其中2可以为Infinity,在Infinity时不管多少层嵌套都可以转化为一维数组
    
  9. flagMap()

    对括号中的函数进行计算,基于原数组,

    let msg = [        1,2,3,6,7      ]      let result = msg.flatMap((x) => [x*2])      console.log(result);      //输出2 4 6 12 14
    
  10. 数组的空位

    1. Array

      new Array();new Array(size);    //size=3 返回(3) [空 ×3]new Array(element0, element1, ..., elementn);
      
    2. Array.from方法会将数组的空位,转为undefined

    3. for...of

      let arr = [,,1,2,,]      for (let i of arr){        console.log(i);      }//输出结果undefinedundefined12undefined
      
    4. entries()keys()values()find()findIndex()会将空位处理成undefined

      let arr = [...[,'a'].entries()]console.log(arr)//(2) [Array(2), Array(2)]0: (2) [0, undefined]1: (2) [1, "a"]
      
  11. sort排序

          let arr = [
            'abs',
            'bsf',
            'cdfd',
            'dosie'
          ]
          const sorting = (s1,s2) => {
            if(s1[0] < s2[0])  return -1;
            return 1;       
          }
          let a = arr.sort(sorting)
          console.log(arr)