我试图创建一个对象数组,但是当我将其推入数组时,它是在添加对对象的引用而不是复制值。
var nestedOrgList = [];
var tempTopOrg = {};
var i = 0;
while (typeof divList[i] !== 'undefined') {
tempTopOrg.org = divList[i++]; // increment i after we assign this
tempTopOrg.suborgs = [];
while ($(divList[i]).has('.expand').length < 1 && i < divList.length) {
tempTopOrg.suborgs.push(divList[i++]);
}
nestedOrgList.push(tempTopOrg);
};
有一个更好的方法吗?还是需要手动复制?
nestedOrgList[insertIndex].org = tempTopOrg.org;
// etc..
insertIndex++;
最佳答案
您可以检查以下答案
How do I correctly clone a JavaScript object?
JSperf
http://jsperf.com/cloning-an-object/82
绝对可以,JavaScript应该有一种本地复制引用的方法。