我有这个(子)对象叫做products.original
。为什么我的原始对象在我将其“复制”到一个新对象后受到影响?
然后我解压到一个新的对象调用productArray
:
var productArray = [];
productArray = products.original;
/*some calculations here*/
//sort the object by lowest score
productArray.sort(function(a, b) {return a.score - b.score;});
最后,我在productArray
前三细胞中提取到名为resultArray
的第三个目的:
var resultArray= [];
resultArray = productArray.splice(0,3);
令我惊讶的是,这减少了3(拼接)的长度products.original
。为什么?我该怎么做才能阻止呢?提前致谢。
你应该摆脱'= []'。如果要在下一行中将不同的数组分配给该变量,则创建数组并将其分配给变量没有意义。 – Quentin 2012-02-24 10:25:16