[英]Why can't I build this Javascript object?
I have an object I'm trying to populate from another object (that is, iterate over a return object to produce an object with only selected values from the original). 我有一个对象我正在尝试从另一个对象填充(也就是说,迭代一个返回对象来生成一个只包含原始选定值的对象)。 My code looks like this:
我的代码看起来像这样:
var collect = {};
function getHistoricalData(username){
$.getJSON("http://url/" + username + ".json?params",
function(data){
for (var i=0; i < data.length; i++) {
console.log(i);
collect = { i : {text : data[i].text}};
$("#wrap").append("<span>" + data[i].text + "</span><br />");
};
console.log(collect);
});
}
So I'm using Firebug for debugging, and here's what I know: 所以我使用Firebug进行调试,这就是我所知道的:
console.log(i);
is showing the numbers 1-20 as expected When I log the collect
object at the end, it's structure is this: var collect = { i : {text : "the last iteration's text"}};
当我在最后记录
collect
对象时,它的结构如下: var collect = { i : {text : "the last iteration's text"}};
So the incrementer is "applying" to the data[i].text and returning the text value, but it's not doing what I expected, which is create a new member of the collect object; 因此,增量器“应用”到data [i] .text并返回文本值,但它没有按照我的预期进行,这是创建了collect对象的新成员; it's just overwriting
collect.i
20 times and leaving me with the last value. 它只是覆盖
collect.i
20次并留下最后一个值。
Is there a different syntax I need to be using for assigning object members? 我需要使用不同的语法来分配对象成员吗? I tried
collect.i.text =
and collect[i].text =
and the error was that whatever I tried was undefined. 我尝试了
collect.i.text =
并collect[i].text =
,错误是我尝试的任何内容都是未定义的。
I'd love to know what's going on here, so the more in-depth an explanation the better. 我很想知道这里发生了什么,所以解释越深入越好。
Thanks! 谢谢!
The reason you're seeing only one property in the object at the end is that you're not augmenting it in each loop iteration - you're overwriting it. 你最终只在对象中看到一个属性的原因是你没有在每次循环迭代中扩充它 - 你要覆盖它。
collect = { i : {text : data[i].text}};
This says: create an object with one property, i
, whose value is another object with one property, text
, and assign it to the variable collect
. 这说:创建一个具有一个属性的对象,
i
,其值是具有一个属性, text
另一个对象,并将其分配给变量collect
。 What you want to do instead is set a property of the existing collect
object for each value of i
, where i
is the name of the property. 您想要做的是为
i
每个值设置现有collect
对象的属性,其中i
是属性的名称。
In order to create an object property where the name is set dynamically, you must use array-style syntax, like this: collect[i]
. 为了创建一个动态设置名称的对象属性,必须使用数组样式语法,如下所示:
collect[i]
。 I don't think you can mix array syntax and object literal syntax like you've tried. 我不认为你可以像你尝试过的那样混合数组语法和对象文字语法。 Try this instead (disclaimer, not tested):
试试这个(免责声明,未经测试):
collect[i] = { text: data[i]["text"] };
collect[i] = {text : data[i].text};
If you use collect[i].text
, since collect[i]
does not exist before the assignment, it will return undefined
, which has no text
property. 如果你使用
collect[i].text
,因为在赋值之前collect[i]
不存在,它将返回undefined
,它没有text
属性。
Also: 也:
collect.i
means collect["i"]
. collect.i
表示collect["i"]
。 This is totally different from collect[i]
. collect[i]
完全不同。 .push
. .push
。 As you already know, the problem is in this line: 如您所知,问题出在这一行:
collect = { i : {text : data[i].text}};
Every iteration of the loop, you are erasing the collect object and assigning it a new object as value. 循环的每次迭代,您正在擦除collect对象并为其指定一个新对象作为值。 That's why in the end it only shows the last iteration.
这就是为什么最后它只显示最后一次迭代。
Try this: 尝试这个:
collect[i] = {text : data[i].text};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.