数组层级较深时,需要使用深拷贝
//浅拷贝
var test = {
a: 'a',
b: 'b'
};
var test2 = test;
//深拷贝--1
function deepClone(data) {
var t = type(data), o, i, ni;
if(t === 'array') {
o = [];
}else if( t === 'object') {
o = {};
}else {
return data;
}
if(t === 'array') {
for (i = 0, ni = data.length; i < ni; i++) {
o.push(deepClone(data[i]));
}
return o;
}else if( t === 'object') {
for( i in data) {
o[i] = deepClone(data[i]);
}
return o;
}
}
//深拷贝--2
let content = [{
attr: "day",
content_type: "1",
value: [],
period: {
last: 7
}
}]
let arr = JSON.parse(JSON.stringify(content));