ES6——数组

  • 数组创建

    • Array.of()

      将参数中所有值作为元素形成数组

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

      将类数组对象或可迭代对象转化为数组

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

      参数

      Array.from(arrayLike[,mapFn[,thisArg]])

      • arrayLike

        想要转换的类数组对象或可迭代对象

      • mapFn

        可选,map 函数,对每个元素进行处理

      • thisArg

        可选,用于指定 map 函数执行时的 this 对象

      Array.from([1,2,3],(n)=>n*2)	//[2,4,6]
      let map = {
          do: function(n){
              return n*2
          }
      }
      let arrayLike = [1,2,3]
      Array.from(arrayLike,function(n){
          return this.do(n)
      },map)	//[2,4,6]	指定 this 为 map
      
    • 类数组对象

      一个类数组对象必须包含 length 属性,且元素属性名必须是数值或者可转换为数值的字符

    • 转换可迭代对象

      • 转换 map

        let map = new Map()
        map.set('key0','value0')
        map.set('key1','value1')
        Array.from(map)	//[['key0','value0'],['key1','value0']]
        
      • 转换 set

      • 转换 字符串

  • 扩展方法

    • 查找

      • find()

        超找数组中符合条件的元素,若有多个则返回第一个

        let arr = Array.of(1,2,3,4)
        arr.find(n=>n>2)	//3
        [,1].find(n=>true)	//undefined
        
      • findIndex()

        查找数组中符合条件的元素索引,若有多个符合条件的元素则返回第一个元素索引

        参数1:回调函数

        参数2:回调函数中的 this 的指向

    • 填充

      • fill()

        将一定范围索引的数组元素内容填充为单个指定的值

        参数1:用来填充的值

        参数2:被填充的起始索引

        参数3(可选):被填充的结束索引,默认为数组末尾

        [1,2,3,4].fill(0,1,2)		//[1,0,3,4]		//指定末尾是开区间
        
      • copyWithin()

        将一定范围索引的数组元素修改为此数组另一指定范围索引的元素

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

        参数1:修改元素的起始位置

        参数2:指定范围的起始位置

        参数3:可选,指定范围的末尾位置,开区间

    • 遍历

      • entries()

        遍历键值对

        for(let [key, value] of ['a', 'b'].entries()){
            console.log(key, value);
        }	
        //0 'a'
        //1 'b'
        
      • keys()

        遍历键名

        for(let key of ['a','b'].keys()){
            console.log(key)
        }
        //0
        //1
        
      • values()

        遍历值

        for(let value of ['a','b'].values()){
            console.log(value)
        }
        
    • 包含

      • includes()

        数组是否包含指定值,

        与 Set 和 Map 的 has 方法区分;Set 的 has 方法用于查找值;Map 的 has 方法用于查找键名

        [1,2,3].includes(1)	//true
        [1,2,3].includes(1,2)	//false
        [1,NaN,3].includes(NaN)	//true	可以判断 NaN 的包含
        

        参数1:包含的指定值

        参数2:可选,搜索的歧视索引,默认为0

    • 数组嵌套转一维数组

      • flat()

        [1,[2,3]].flat()		//[1,2,3]
        [1,[2,[3,[4,5]]]].flat(2)	//[1,2,3,[4,5]]
        [1,[2,[3,[4,5]]]].flat(Infinity)	//[1,2,3,4,5]	不管多少层
        [1,[2,,3]].flat()	//[1,2,3]	自动跳过空位
        

        参数1:可选,指定嵌套层数,默认为1

      • flatMap()

        先对数组中的每个元素进行处理,再对数组中的每一个元素执行 flat() 方法

        console.log([1,2,3].flatMap(n=>n*2))	//[2,4,6]
        

        参数1:遍历函数(这个函数可以接受3个参数:当前元素,当前元素索引,原数组)

        参数2:指定遍历函数中 this 指向

  • 扩展运算符

    • 复制数组 ...

      let arr = [1,2],
          arr1 = [...arr]
      console.log(arr1)	//[1,2]
      
      //数组含空位
      let arr2 = [1,,3],
          arr3 = [...arr2]
      console.log(arr3)	//[1,undefined,3]
      
    • 合并数组

      [...[1,2],...[3,4]]		//[1,2,3,4]
      
  • 数组缓冲区

    数组缓冲区是内存中的一段地址,定型数组的基础,实际字节数在创建时确定,之后只可修改其中的数据,不可修改其大小