We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[]
console.log(...[1, 2, 3]) // 1 2 3
const arr = [ ...(x > 0 ? ['a'] : []), 'b', ];
const a1 = [1, 2]; // 写法一 const a2 = [...a1]; // 写法二 const [...a2] = a1;
Array.from方法将:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括 ES6 新增的数据结构 Set 和 Map)两类对象转为真正的数组。 常见的类数组有arguments和NodeList
arguments
NodeList
let arrayLike = { '0': 'a', '1': 'b', '2': 'c', length: 3 }; // ES5的写法 var arr1 = [].prototype.slice.call(arrayLike); // ['a', 'b', 'c'] // ES6的写法 let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']
Array.from还可以接受第二个参数,作用类似于数组的map方法,用来对每个元素进行处理,将处理后的值放入返回的数组。
map
Array.from(arrayLike, x => x * x); // 等同于 Array.from(arrayLike).map(x => x * x); Array.from([1, 2, 3], (x) => x * x) // [1, 4, 9]
Array.of方法用于将一组值转化为数组
Array.of(3, 11, 8) // [3,11,8] Array.of(3) // [3] Array.of(3).length // 1
弥补数组构造函数Array()的不足,只有当参数个数不少于 2 个时,Array()才会返回由参数组成的新数组。参数个数只有一个时,实际上是指定数组的长度。
Array()
Array() // [] Array(3) // [, , ,] Array(3, 11, 8) // [3, 11, 8]
rest参数同理:
rest
(function(...args) {}).length // 0
扩展运算符 ...
常见的类数组:arguments、NodeList
Array.from(arr, fn(ele,index)) --返回一个新数组
类数组
Array.of(a,b,c) //[a,b,c]
length
上一篇:ES6总结系列之 函数的扩展 篇 下一篇:ES6总结系列之 Symbol 篇
The text was updated successfully, but these errors were encountered:
No branches or pull requests
ES6总结系列之 数组的扩展 篇
1.扩展运算符(...)
①扩展运算符(spread)好比rest参数的逆运算,将一个数组转化为逗号分隔的参数序列,可以想象成去掉中括号
[]
②扩展运算符后面还可以放置表达式
③可以实现数组深拷贝
2.Array.from(arr, fn(item,index)) --返回一个新数组
Array.from方法将:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括 ES6 新增的数据结构 Set 和 Map)两类对象转为真正的数组。
常见的类数组有
arguments
和NodeList
Array.from还可以接受第二个参数,作用类似于数组的
map
方法,用来对每个元素进行处理,将处理后的值放入返回的数组。3.Array.of(a,b,c) //[a, b, c]
Array.of方法用于将一组值转化为数组
弥补数组构造函数
Array()
的不足,只有当参数个数不少于 2 个时,Array()
才会返回由参数组成的新数组。参数个数只有一个时,实际上是指定数组的长度。rest
参数同理:总结要点
扩展运算符 ...
常见的类数组:arguments、NodeList
Array.from(arr, fn(ele,index)) --返回一个新数组
类数组
(具有length属性就行)和可遍历的对象(具有Iterat)转换为数组Array.of(a,b,c) //[a,b,c]
length
的缺陷上一篇:ES6总结系列之 函数的扩展 篇
下一篇:ES6总结系列之 Symbol 篇
The text was updated successfully, but these errors were encountered: