对象数组的克隆 "sub-array"(其中对象嵌套很深)
Clone "sub-array" of an array of objects (where objects are deeply nested)
问题1.我想知道为什么
JSON.parse(JSON.stringify(obj.slice(1, 3))) and
obj.slice(1,3)
给出与输出相同的嵌套对象数组,因为 obj.slice(1,3)
不应该正确克隆嵌套对象?
问题2. JSON.parse(JSON.stringify(obj.slice(1, 3)))
深度克隆子数组的正确方法是?
对象详情-
var obj= [{ name: "wfwfwfw.)csdfsd",
tags: [ "dfbdf>>sfdfds", "fsdfsdf&fsfd" ],
newer: { first: "this'one", second: ["that>.one", "another.'one"], third: {something: "some/>fded", newthing: "ddasd..>sqw"} },
final: [ {gh: "ty/fgfg", hj: "rt((ssds"}, {gh: "dqqq...g", hj: "gnm))s"} ]
},
{ name: "wfwfwwwwwwfw.)csdfsd",
tags: [ "dfbdf>>sfdfds", "fsdfsdf&fsfd" ],
newer: { first: "this'one", second: ["that>.one", "another.'one"], third: {something: "some/>fded", newthing: "ddasd..>sqw"} },
final: [ {gh: "ty/fgfg", hj: "rt((ssds"}, {gh: "dqqq...g", hj: "gnm))s"}]
},
{ name: "aa.)csdfsd",
tags: [ "dfbdf>>sfdfds", "fsdfsdf&fsfd" ],
newer: { first: "this'one", second: ["that>.one", "another.'one"], third: {something: "some/>fded", newthing: "ddasd..>sqw"} },
final: [ {gh: "ty/fgfg", hj: "rt((ssds"}, {gh: "dqqq...g", hj: "gnm))s"}]
},
{ name: "nn.)csdfsd",
tags: [ "dfbdf>>sfdfds", "fsdfsdf&fsfd" ],
newer: { first: "this'one", second: ["that>.one", "another.'one"], third: {something: "some/>fded", newthing: "ddasd..>sqw"} },
final: [ {gh: "ty/fgfg", hj: "rt((ssds"}, {gh: "dqqq...g", hj: "gnm))s"}]
}]
如果 JSON.parse(JSON.stringify())
来自 obj.slice(1, 3)
的结果,为什么会有所不同?这就是示例 1 中发生的情况。
你得到 obj.slice(1, 3)
,你得到 相同的值,通过字符串化/解析。
在功能上,这类似于:
var foo = 'bar';
console.log(foo);
console.log(JSON.parse(JSON.stringify(foo)));
关于问题 2:
是的。
obj.slice
不能单独用于克隆对象。它只是复制数组的 contents。在这种情况下,这些内容只是指向现有对象的指针:
var a = [{foo: 'bar'}, {x: 'y'}];
var b = a.slice(0,1)
console.log('B:', b);
b[0].foo = 'test';
console.log('After modification:');
console.log('A:', a);
console.log('B:', b);
如您所见,在 b
上编辑 foo
也会在 a
上更改它,因为两个数组都包含指向 same[=51= 的指针] 对象。
这就是为什么您需要 JSON.parse(JSON.stringify())
。
Array.slice() 不进行深度复制,因此不适用于多维数组。
jQuery 的 extend 方法确实在将真值作为初始参数传递时执行深度复制。
// Deep copy
var newArray = jQuery.extend(true, [], oldArray);
相似posthere
问题1.我想知道为什么
JSON.parse(JSON.stringify(obj.slice(1, 3))) and
obj.slice(1,3)
给出与输出相同的嵌套对象数组,因为 obj.slice(1,3)
不应该正确克隆嵌套对象?
问题2. JSON.parse(JSON.stringify(obj.slice(1, 3)))
深度克隆子数组的正确方法是?
对象详情-
var obj= [{ name: "wfwfwfw.)csdfsd",
tags: [ "dfbdf>>sfdfds", "fsdfsdf&fsfd" ],
newer: { first: "this'one", second: ["that>.one", "another.'one"], third: {something: "some/>fded", newthing: "ddasd..>sqw"} },
final: [ {gh: "ty/fgfg", hj: "rt((ssds"}, {gh: "dqqq...g", hj: "gnm))s"} ]
},
{ name: "wfwfwwwwwwfw.)csdfsd",
tags: [ "dfbdf>>sfdfds", "fsdfsdf&fsfd" ],
newer: { first: "this'one", second: ["that>.one", "another.'one"], third: {something: "some/>fded", newthing: "ddasd..>sqw"} },
final: [ {gh: "ty/fgfg", hj: "rt((ssds"}, {gh: "dqqq...g", hj: "gnm))s"}]
},
{ name: "aa.)csdfsd",
tags: [ "dfbdf>>sfdfds", "fsdfsdf&fsfd" ],
newer: { first: "this'one", second: ["that>.one", "another.'one"], third: {something: "some/>fded", newthing: "ddasd..>sqw"} },
final: [ {gh: "ty/fgfg", hj: "rt((ssds"}, {gh: "dqqq...g", hj: "gnm))s"}]
},
{ name: "nn.)csdfsd",
tags: [ "dfbdf>>sfdfds", "fsdfsdf&fsfd" ],
newer: { first: "this'one", second: ["that>.one", "another.'one"], third: {something: "some/>fded", newthing: "ddasd..>sqw"} },
final: [ {gh: "ty/fgfg", hj: "rt((ssds"}, {gh: "dqqq...g", hj: "gnm))s"}]
}]
如果 JSON.parse(JSON.stringify())
来自 obj.slice(1, 3)
的结果,为什么会有所不同?这就是示例 1 中发生的情况。
你得到 obj.slice(1, 3)
,你得到 相同的值,通过字符串化/解析。
在功能上,这类似于:
var foo = 'bar';
console.log(foo);
console.log(JSON.parse(JSON.stringify(foo)));
关于问题 2:
是的。
obj.slice
不能单独用于克隆对象。它只是复制数组的 contents。在这种情况下,这些内容只是指向现有对象的指针:
var a = [{foo: 'bar'}, {x: 'y'}];
var b = a.slice(0,1)
console.log('B:', b);
b[0].foo = 'test';
console.log('After modification:');
console.log('A:', a);
console.log('B:', b);
如您所见,在 b
上编辑 foo
也会在 a
上更改它,因为两个数组都包含指向 same[=51= 的指针] 对象。
这就是为什么您需要 JSON.parse(JSON.stringify())
。
Array.slice() 不进行深度复制,因此不适用于多维数组。
jQuery 的 extend 方法确实在将真值作为初始参数传递时执行深度复制。
// Deep copy
var newArray = jQuery.extend(true, [], oldArray);
相似posthere