❤推荐1. JSON.parse(JSON.stringify())
let a = {
name: 'JJ',
age: 18
}
b = JSON.parse(JSON.stringify(a))
b.age = 25;
console.log(a, b);
2.迭代递归方法
deepCopy(obj) {
let objArray = Array.isArray(obj) ? [] : {};
if (obj && typeof obj === "object") {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
if (obj[key] && typeof obj[key] === "object") {
objArray[key] = this.deepCopy(obj[key]);
} else {
objArray[key] = obj[key];
}
}
}
}
return objArray;
}
3.jQuery中extend( )
jQuery.extend([deep], target, object1, [objectN]);
实例:
let obj = {
childs: ['Jack', 'Tom'],
age: 45
}
let newObj = $.extend(true, {}, obj)
newObj.childs[0] = 'Jone'
console.log(newObj.childs[0]) //----'Jone'
console.log(obj.childs[0]) //-----'Jack'
4.lodash工具库
引入:
import _ from 'lodash'
使用:
const b=_.cloneDeep(a)