JS 复制新数组的方法
需求:将旧数组复制新数组,不是引用
方法1:
arr = [1,2,3];
let newArr = new Array();
arr.forEach((item) => newArr.push(item));
方法2:
arr = [1,2,3];
let newArr = arr.slice();
需求:将旧数组复制新数组,不是引用
方法1:
arr = [1,2,3];
let newArr = new Array();
arr.forEach((item) => newArr.push(item));
方法2:
arr = [1,2,3];
let newArr = arr.slice();