es6 --- 数组的扩展
生活随笔
收集整理的這篇文章主要介紹了
es6 --- 数组的扩展
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
經常遇到對數組的操作…下面是《ES6標準入門》(第3版)中對數組擴展的(部分)描述:
擴展運算符(…):
擴展運算符代替數組的apply方法
// ES5 function f(x,y,z) {// ... } var args = [1,2,3]; f.apply(null, args);// ES6 function f(x,y,z) {// ... } var args = [0,1,2] f(...args);// 可見,調用更清晰Math.max
// ES5 Math.max.apply(null, [14, 3, 77])// ES6 Math.max(...[14, 3, 77])// 等同于 Math.max(14, 3, 77)合并數組:
// ES5 var arr1 = [0,1,2]; var arr2 = [3,4,5]; Array.prototype.push.apply(arr1, arr2);// ES6 const arr1 = [0,1,2]; const arr2 = [3,4,5]; arr1.push(...arr2);與解構賦值結合使用:
// ES5 a = list[0], rest = list.slice(1)// ES6 [a, ...rest] = list將字符串轉換成真正的數組:
const str = 'hello'; const arr = [...str];Map
const map = new Map([[1, 'one'],[2, 'two'],[3, 'three'] ]) const keys = [...map.keys()]; // [1, 2, 3] const values = [...map.values()]; // ["one", "two", "three"]總結
以上是生活随笔為你收集整理的es6 --- 数组的扩展的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手把手教你做做一份高质量投标书
- 下一篇: 张掖计算机职称考试,甘肃省张掖市2018