Loading

数组

扩展运算符 (...)

  • 放数组、表达式

    cosnt arr = [...arr1,...(x>2?['1']:['2'])]
    
  • 替代数组的apply方法(用于将数组作为参数传入函数中)

    function f(x,y,z){}
    f.apply(null,arrs)  //es5写法
    f(...arrs) //es6
    
  • 合并数组

    const arr3 = [...arr1,...arr2]
    
  • 结构赋值

    a=1
    rest = [2,3,4,5]
    [a,...rest] = list  //list=[1,2,3,4,5],数组赋值时只能放在参数的最后一位
    
  • 在计算字符串的长度时,最好用扩展运算符,因为扩展运算符可以正确的识别32位的unicode字符

    var hello = 'hellojs'
    [...hello].length
    
  • 只要是具有Iterator 接口的对象就可以使用扩展运算符

Array.from(将对象转化为数组)

  • 类似数组的对象(有length 属性)

  • 可遍历的对象(Iterable对象)

Array.of(生成一个数组)

Array 表现 Array.of Array.of
Array() [] Array.of() []
Array(3) [ , , ] Array.of(3) [3]
Array(3,8,10) [3,8,10] Array.of(1,2,3) [1,2,3]

copyWithin(修改当前数组)

  • target:从该位置开始替换数据
  • start:从该位置开始读取数据,负数为倒数
  • end:到该位置前停止读取数据,负数为倒数

findfindIndex

  • find :找到即返回值
  • findIndex :找到即分会值的位置

fill(填充数组)

Array(5).fill(1);//[1,1,1,1,1]
[1,2,3].fill(1);//[1,1,1]
posted @ 2021-11-15 23:20  淡蓝色的点  阅读(31)  评论(0)    收藏  举报