let arr = [ "A", [1, 2, 3], true, null, { age: 18 }];function deepClone(temp, newArr = []) { // 这里可以传入新数组,以及老数组。然后 实参大于形参,所以传入对象的时候会覆盖 //遍历被拷贝的数组 for (let index in temp) { if (temp[index] instanceof Object && temp[index] != null) { // 判断是否是引用数据类型,除去null //实现深拷贝 //这里也可以使用if(){}else{} if(temp[index] instanceof Array ?){ newArr[index] = [] }else{ newArr[index] ={} } //主要还是使用三目运算,简单 newArr[index] = temp[index] instanceof Array ? [] : {}; // 使用三目运算 //这里使用递归 deepClone(temp[index], newArr[index]); } else { //如果传入的是对象 eg:newArr[name] = temp[name] //先给新对象创建 键名,然后再将 值赋值过去 newArr[index] = temp[index]; } } return newArr;}let cloneArr = deepClone(arr);console.log(arr);console.log(cloneArr);//进行验证// arr[1][1] = "B";// arr[3].age = 20;console.log(ar2r);console.log(cloneArr);