[英]Javascript array with for loop, returns only last element
I have a for
loop, that adds data into an array
.我有一个
for
循环,它将数据添加到array
。 but when I console.log
the array, it is full of the last item of the for
loop!但是当我
console.log
数组时,它充满了for
循环的最后一项!
Here is my code :这是我的代码:
var materialsData = results[1].data, // results[1].data is a http.get return
ln = Object.size(materialsData),
materials = [],
material = {};
material['Product'] = {};
for (var i = 0; i < ln; i++) {
material.Product['Name'] = materialsData[i].Product.Name;
material.Product['Id'] = materialsData[i].Product.Id;
material.StartingDate = materialsData[i].StartingDate.replace("T00:00:00", "").split('-').reverse().join('-');
material.Device = materialsData[i].Device;
materials.push(material);
}
You are updating and pushing the same object reference again and again so the object holds the last element values. 您要一次又一次更新并推送相同的对象引用,以便该对象保留最后一个元素值。 Instead, initialize the object holding variable inside the for loop beginning.
相反,请在for循环开始处初始化包含对象的变量。
for(var i=0; i<ln; i++){
// initialize the object
var material = { Product : {}, Id : {}};
material.Product['Name'] = materialsData[i].Product.Name;
material.Product['Id'] = materialsData[i].Product.Id;
material.StartingDate = materialsData[i].StartingDate.replace("T00:00:00", "").split('-').reverse().join('-');
material.Device = materialsData[i].Device;
materials.push(material);
}
Or directly define the object as the argument of push method without holding it to any variable. 或者直接将对象定义为push方法的参数,而不将其保留在任何变量中。
for (var i = 0; i < ln; i++) {
materials.push({
Product: {
Name: materialsData[i].Product.Name,
Id: materialsData[i].Product.Id,
},
StartingDate: materialsData[i].StartingDate.replace("T00:00:00", "").split('-').reverse().join('-'),
Device: materialsData[i].Device
})
}
Define material
in the for
block. 在
for
块中定义material
。 As Objects are passed by reference same object
is updated and pushed to the array. 当对象通过引用传递时,同一
object
将更新并推送到数组。
for (var i = 0; i < ln; i++) {
var material = {
Product : {
Name : materialsData[i].Product.Name,
Id : materialsData[i].Product.Id,
},
StartingDate : materialsData[i].StartingDate.replace("T00:00:00", "").split('-').reverse().join('-'),
Device : materialsData[i].Device
};
materials.push(material);
}
Additionally, You can use Array.map()
此外,您可以使用
Array.map()
var materials = materialsData.map(function(m){
return {
Product : {
Name : m.Product.Name,
Id : m.Product.Id,
},
StartingDate : m.StartingDate.replace("T00:00:00", "").split('-').reverse().join('-'),
Device : m.Device
};
})
You are indeed referencing the same object.您确实在引用同一个对象。 For me the trick was to wrap the object in question around
JSON.stringify()
and then within the loop I call JSON.parse()
on the resulting string to turn it back对我来说,诀窍是将有问题的对象包装在
JSON.stringify()
周围,然后在循环中我在结果字符串上调用JSON.parse()
以将其返回
var materialsDataString = JSON.stringify(results[1].data), // results[1].data is a http.get return ln = Object.size(materialsData), materials = [], material = {}; material['Product'] = {}; for (var i = 0; i < ln; i++) { var materialsData = JSON.parse(materialsDataString) material.Product['Name'] = materialsData[i].Product.Name; material.Product['Id'] = materialsData[i].Product.Id; material.StartingDate = materialsData[i].StartingDate.replace("T00:00:00", "").split('-').reverse().join('-'); material.Device = materialsData[i].Device; materials.push(material); }
. .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.