数组深复制的方式 发表于 2019-08-23 | 分类于 js 数组的深复制在开发过程中会经常用到,总结记录 1、用数组的slice()12345var arr1 = [1,2,3];var arr2 = arr1.slice(0);arr1[0]=0;console.log(arr1); // [0,2,3]console.log(arr2); // [1,2,3] 2、用数组的concat()12345var arr1 = [1,2,3];var arr2 = arr1.concat();arr1[0]=0;console.log(arr1); // [0,2,3]console.log(arr2); // [1,2,3] 3、用ES6扩展运算符12345var arr1 = [1,2,3];var arr2 = [...arr1];arr1[0] = 0;console.log(arr1); // [0,2,3]console.log(arr2); // [1,2,3] 4、用JSON.parse(JSON.stringify())12345var arr1 = [1,2,3];var arr2 = JSON.parse(JSON.stringify(arr1));arr1[0] = 0;console.log(arr1); // [0,2,3]console.log(arr2); // [1,2,3]