javascript数组push方法覆盖for循环中的值。 [英] javascript array push method overwrites the value in a for loop.
本文介绍了javascript数组push方法覆盖for循环中的值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
i我正在使用array.push方法将项目添加到数组中,但每次都会给我最后一个值,而不是我从对象获得的所有值,
这是我的代码。
Hi,
i am using array.push method to add items to an array but every times it gives me last value and not all the values which i am getting from object,
here is my code.
function generatearray(obj)
{
jsonObjEdt = [];
item = {};
for (j = 0; j < obj.length; j++) {
$.each(obj[j], function (key, value) {
var id = key;
var Size = value;
item[id] = Size;
});
jsonObjEdt.push(item);
}
}
我的obj有5个项目,我的jsonObjEdt也得到5个值,但所有五个值都是最后来自obj。不确定为什么会这样?
my obj have 5 items and my jsonObjEdt also get 5 values but all five values are last from the obj. not sure why this happens ?
推荐答案
.each(obj [j], function (键,值) ){
var id = key;
var 大小=值;
item [id] =尺寸;
});
jsonObjEdt.push(item);
}
}
.each(obj[j], function (key, value) { var id = key; var Size = value; item[id] = Size; }); jsonObjEdt.push(item); } }
我的obj有5个项目,我的jsonObjEdt也得到5个值,但所有五个值都是最后来自obj。不确定为什么会这样?
my obj have 5 items and my jsonObjEdt also get 5 values but all five values are last from the obj. not sure why this happens ?
Hi,
.each本身就是一个循环为什么是你用它来循环吗?
只需使用
.each itself is a loop why are you using for loop over it??
Just use
这篇关于javascript数组push方法覆盖for循环中的值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文