es6数组新增函数from()和of()
from()将可迭代结构转换为数组
console.log(Array.from('js'))
数组的浅复制
let a1 = ['js', 'html', 'css', {k1: 1, k2: 2}]
let a2 = Array.from(a1)
console.log(a2);
console.log(a1[3] === a2[3]);
转换map
let m = new Map()
m.set('k1', '1')
m.set('k2', '2')
m.set('k3', '3')
m.set('k4', '4')
console.log(Array.from(m));
console.log(Array.from(m.values(), v => 'v' + v));
转换生成器
console.log(Array.from({
* [Symbol.iterator]() {
yield 1
yield 2
}
}))
of()将所有参数转换为数组
console.log(Array.of('4', '3'))
console.log(Array.of(undefined, null))