用扩展运算符对数组浅拷贝
const aTest = [1, 2, 3];
const aTest2 = [...aTest, 4];
console.log(aTest2);
用...运算符而不是Array.from将一个可迭代对象 转换成数组
const oTest = {
data: [1, 2, 3],
[Symbol.iterator]() {
let index = 0;
return {
next: () => {
if (index < this.data.length) {
return {value: this.data[index++], done:false}
} else {
return {done: true}
}
}
}
}
};
const aTest = [...oTest]
console.log(aTest);
const aTest2 = Array.from(oTest1)
console.log(aTest2);
const oTest3 = {0:1, 1:2, 2:3, length:3};
const aTest3 = Array.from(oTest3);
console.log(aTest3);
用Array.from而不是...运算符去做map遍历可避免创建新数组
const aTest = [1, 2, 3];
const aTest1 = item => item + 1;
const aTest2 = [...aTest].map(aTest1);
const aTest3 = Array.from(aTest, aTest1);
console.log('aTest2',aTest2);
console.log('aTest3',aTest3);