006 数组扩展_扩展运算符

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

console.log(...[1,2,3])

//1 2 3

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

//1 2 3 4 5

替代函数的apply方法

由于扩展运算符可以展开数组,所以不在需要apply方法,将数组转为函数的参数了

//ES5的写法

Math.max.apply(null,[14,3,77])

//ES6的写法

Math.max(...[14,3,77])

//等同于

Math.max(14,3,77);

2、合并数组

扩展运算符提供了数组合并的新写法

  const arr1=['a','b'];

  const arr2=['c'];

  const arr3=['d','e'];

//ES5的合并数组

arr1.concat (arr2,arr3);

//['a','b','c','d','e']

 

posted @ 2025-02-10 16:53  张筱菓  阅读(9)  评论(0)    收藏  举报