实现一个数组的深拷贝有几种写法,分别是什么?

terry Javascript 378

实现一个数组的深拷贝有几种写法,分别是什么?

1.for循环
let arr = [1,2,3,4,5];
let arr1 = [];
for(let i = 0;i < arr.length;i++){
    arr1[i]  = arr[i];
}
console.log(arr1);//[1, 2, 3, 4, 5]

2.concat();
let arr2 = [].concat(arr);
console.log(arr2);//[1, 2, 3, 4, 5]

3.   ...
let arr3 = [...arr];
let [...arr4] = arr;
console.log(arr3);//[1, 2, 3, 4, 5]
console.log(arr4);//[1, 2, 3, 4, 5]
  • 暂无回复内容