javascript 推送正在覆盖数组中的先前数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19054997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Push is overwriting previous data in array
提问by dlofrodloh
I'm passing a string which looks something like: "John.Doe.100.Newbie-David.Miller.250.Veteran-" to the SplitDatabase function which splits the string appropriately and assigns the values to the UserDataEntry object. The UserDataEntry object is then pushed in to the global UserData array which is supposed to store all the user data.
我将一个类似于“John.Doe.100.Newbie-David.Miller.250.Veteran-”的字符串传递给 SplitDatabase 函数,该函数适当地拆分字符串并将值分配给 UserDataEntry 对象。然后将 UserDataEntry 对象推送到应该存储所有用户数据的全局 UserData 数组中。
For some reason though, the UserData.push(UserDataEntry) part seems to be overwriting previous data in the UserData array. The alert in the 1st loop shows the correct data as it loops, but alert in the second loop at the bottom just shows the last record over and over again.
但出于某种原因, UserData.push(UserDataEntry) 部分似乎覆盖了 UserData 数组中的先前数据。第一个循环中的警报在循环时显示正确的数据,但底部第二个循环中的警报只是一遍又一遍地显示最后一条记录。
I'm not sure why this is?
我不确定这是为什么?
var UserData = [];
function SplitDatabase(result) {
var RawUsers = result.split('-');
var UserDataEntry = {};
for (var i = 0; i < (RawUsers.length - 1); i++) {
var tempUserData = RawUsers[i].split('.');
for (var x = 0; x < (tempUserData.length); x++) {
switch (x) {
case 0:
UserDataEntry.firstname = tempUserData[x];
break;
case 1:
UserDataEntry.lastname = tempUserData[x];
break;
case 2:
UserDataEntry.points = tempUserData[x];
break;
case 3:
UserDataEntry.rank = tempUserData[x];
UserData.push(UserDataEntry);
alert(UserData[i].firstname);
break;
}
}
}
for (var i = 0; i < (UserData.length); i++) {
alert(UserData[i].firstname);
}
}
回答by James McLaughlin
Calling push
will not copy your object, because JavaScript Object
s are passed by reference: you're pushing the same Object
as every array entry.
调用push
不会复制您的对象,因为 JavaScriptObject
是通过引用传递的:您正在推送与Object
每个数组条目相同的内容。
You can fix this easily by moving the var UserDataEntry = {};
insidethe loop body, so that a new object is created each loop iteration:
您可以通过移动循环体var UserDataEntry = {};
内部来轻松解决此问题,以便每次循环迭代都会创建一个新对象:
for (var x = 0; x < (tempUserData.length); x++) {
var UserDataEntry = {};
回答by Scott Mermelstein
Put your line var UserDataEntry = {}
inside the for loop.
将您的行var UserDataEntry = {}
放在 for 循环中。
Right now, you only have one object, and you're setting each part of the array to that object. You overwrite the members in your loop.
现在,您只有一个对象,并且您正在将数组的每个部分设置为该对象。您覆盖循环中的成员。
If you create a new object inside the loop, you'll add all new members in to the array.
如果您在循环内创建一个新对象,则会将所有新成员添加到数组中。
回答by Arindam
var UserDataEntry = {};
- {}is very important.
var UserDataEntry = {};
- {}非常重要。
var UserDataEntry;
-- If we put this in loop still have a same problem.
var UserDataEntry;
-- 如果我们把它放在循环中仍然有同样的问题。