有时候,我读到了有关新方法Object.fromEntries()的建议,该方法在某些浏览器(reference)的较新版本中受支持。在阅读有关内容时,我想到了使用此方法deep-clone
和object
来代替使用JSON.parse(JSON.stringify(obj))
的想法。到目前为止,从我的角度来看,我已经提出了下一种递归方法。
const obj = {
key1: {key11: "key11", key12: "key12", key13: {key131: 22}},
key2: {key21: "key21", key22: "key22"},
key3: "key3",
key4: [1,2,3,4]
}
const cloneObj = (obj) =>
{
if (typeof obj !== "object")
return obj;
else if (Array.isArray(obj))
return obj.slice();
return Object.fromEntries(Object.entries(obj).map(
([k,v]) => ([k, cloneObj(v)])
));
}
// Clone the original object.
let newObj = cloneObj(obj);
// Make changes on the original object.
obj.key1.key11 = "TEST";
obj.key3 = "TEST";
obj.key1.key13.key131 = "TEST";
obj.key4[1] = "TEST";
// Display both objects on the console.
console.log("Original object: ", obj);
console.log("Cloned object: ", newObj);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
但是,对于是否要完成某个对象的deep-clone
,我是否有所遗漏,我感到怀疑。所以我的问题是:
1)我缺少一些重要的东西来完成对象的deep-clone
?
2)在这种方法还可以的情况下,您是否认为这样做比使用JSON.parse()
和JSON.stringify()
更好?
谢谢!
这是更新版本,其中包含答案的反馈:
const obj = {
key1: {key11: "key11", key12: "key12", key13: {key131: 22}},
key2: {key21: "key21", key22: "key22"},
key3: "key3",
key4: [1,2,3,{key: "value"}]
}
const cloneObj = (obj) =>
{
if (Object(obj) !== obj)
return obj;
else if (Array.isArray(obj))
return obj.map(cloneObj);
return Object.fromEntries(Object.entries(obj).map(
([k,v]) => ([k, cloneObj(v)])
));
}
// Clone the original object.
let newObj = cloneObj(obj);
// Make changes on the original object.
obj.key1.key11 = "TEST";
obj.key3 = "TEST";
obj.key1.key13.key131 = "TEST";
obj.key4[1] = "TEST";
obj.key4[3].key = "TEST";
// Display both objects on the console.
console.log("Original object: ", obj);
console.log("Cloned object: ", newObj);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
针对第二个问题添加了效果比较:
Tested on Firefox 66.0.3 (64bits):
Test_JSON: 1923.000ms
Test_cloneObj: 2047.000ms
Tested on Chrome 73.0.3683.103 (64 bits):
Test_JSON: 2276.560ms
Test_cloneObj: 1903.675ms
const cloneObj = (obj) =>
{
if (Object(obj) !== obj)
return obj;
else if (Array.isArray(obj))
return obj.map(cloneObj);
return Object.fromEntries(Object.entries(obj).map(
([k,v]) => ([k, cloneObj(v)])
));
}
// Generate an object.
const getRandom = (min, max) => Math.floor(Math.random() * (max - min) + min);
let obj = {};
for (let i = 0; i < 100000; i++)
{
obj["Array" + i] = Array.from({length: 100}, () => getRandom(0, 1000));
obj["Obj" + i] = {"key": getRandom(0, 1000)};
obj["Const" + i] = "some_string";
}
// Test performance on JSON.parse()/stringify()
console.time("Test_JSON");
let obj1 = JSON.parse(JSON.stringify(obj));
console.timeEnd("Test_JSON");
// Test performance on cloneObj().
console.time("Test_cloneObj");
let obj2 = cloneObj(obj);
console.timeEnd("Test_cloneObj");
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
答案 0 :(得分:1)
您缺少一件事:
else if (Array.isArray(obj))
return obj.slice();
这将返回数组的浅表副本。如果数组包含对象,则不会克隆这些基础对象:
const obj = [
['foo']
];
const cloneObj = (obj) =>
{
if (typeof obj !== "object")
return obj;
else if (Array.isArray(obj))
return obj.slice();
return Object.fromEntries(Object.entries(obj).map(
([k,v]) => ([k, cloneObj(v)])
));
}
// Clone the original object.
let newObj = cloneObj(obj);
// Make changes on the original object.
obj[0][0] = 'bar';
// Display both objects on the console.
console.log("Original object: ", obj);
console.log("Cloned object: ", newObj);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
要解决此问题,请改用return obj.map(cloneObj);
:
const obj = [
['foo']
];
const cloneObj = (obj) =>
{
if (typeof obj !== "object")
return obj;
else if (Array.isArray(obj))
return obj.map(cloneObj);
return Object.fromEntries(Object.entries(obj).map(
([k,v]) => ([k, cloneObj(v)])
));
}
// Clone the original object.
let newObj = cloneObj(obj);
// Make changes on the original object.
obj[0][0] = 'bar';
// Display both objects on the console.
console.log("Original object: ", obj);
console.log("Cloned object: ", newObj);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
答案 1 :(得分:1)
obj.slice()
替换为obj.map(o => cloneObj(o))
。JSON.parse(JSON.stringify(obj))
更快-在JSBench和JSON
上进行的三个测试每次的运行速度都降低了10%以上。答案 2 :(得分:1)
如上所述,您的数组不是深度克隆-请使用obj.map(cloneObj)
而不是obj.slice()
。
但是另一个疏忽是typeof obj !== "object"
,它不适用于null
。最好使用Object(obj) !== obj
。