数组的扩展

2018-08-21  本文已影响0人  陶六六

数组的扩展

  1. 扩展运算符...Array类似 ...restrest参数的逆运算,所有实现iterator接口的类数组对象都可以扩展成数组对象
        /*...array 展开的参数个数等于array.length*/
        ...[] //0
        [].length === 0
        ...[undefined] //1
        [undefined].length === 1
        ...[undefined,[],1] //3
        [undefined,[],1].length === 3
    
  2. Array.from(ArrayLike,mapFunction)
    • 将类数组转化为数组,存在length属性
          Array.from({length:3,1:2})
          /* [undefined,2,undefined]*/
      
  3. Array()参数数量不同行为表现不一致,Array.of用来替换Array()
        Array.of(3) //[3]
        Array.of(1,2,3) // [1,2,3]
        Array.of()//[]
        Array.of(undefined)//[undefined]
        Array(3) //[,,]
        Array(1,2,3)//[1,2,3]
        Array()//[]
        Array(undefined)//[undefined]
    
  4. 数组实例方法
    • Array.prototype.copyWithin(target,start=0,end=-1)//覆盖原数组
    • Array.prototype.find(filterFunc)// 返回匹配的第一个
    • Array.prototype.findIndex(filterFunc)//返回匹配的第一个的序数
    • Array.prototype.fill(key)
    • Array.prototype.entries(),Array.prototype.keys(),Array.prototype.values()
    • Array.prototype.includes(key,start=0)
    • Array.prototype.flat(times=1)// 不改变原数组
    • Array.prototype.flatMap

tips

  1. 需要传入方法作为参数的实例方法,都可以在参数最后选传一个对象作为传入方法中this的指定对象
上一篇 下一篇

猜你喜欢

热点阅读