这可能是一个非常基本的问题 - 看一个简单的代码:
var ar1=[];
var ar2=[];
ar1[0] = 'Constant';
ar1[1] = data.attr.const;
ar2.push(ar1); //OK, ar2 contains one array of two correct elements
ar1[0] = 'R-squared';
ar1[1] = data.attr.rsq;
ar2.push(ar1); // Not OK - ar2 contains 2 identical arrays
ar1[0] = 'R-sq. adjusted';
ar1[1] = data.attr.rsqadj;
ar2.push(ar1); // Not OK - ar2 contains 3 identical arrays
问题是每次执行ar2.push(ar1)时,都会覆盖 ar2 的所有元素。执行此代码后,我得到一个包含 3 个相同数组的对象。我该如何解决?
谢谢