简体   繁体   English

为什么我不能构建这个Javascript对象?

[英]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进行调试,这就是我所知道的:

  • The JSON object is intact JSON对象完好无损
  • console.log(i); is showing the numbers 1-20 as expected 按预期显示数字1-20
  • 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: 也:

  1. collect.i means collect["i"] . collect.i表示collect["i"] This is totally different from collect[i] . 这与collect[i]完全不同。
  2. You can use an Array and append with .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.

相关问题 为什么不能在具有JavaScript对象的对象中设置此变量? - Why can't I set this variable in an object with an object with javascript? 为什么我不能在.NET中创建COM对象,却可以在JavaScript中创建COM对象 - Why I can't create COM object in .NET but can in JavaScript 为什么不能在JavaScript中将对象属性设置为空字符串? - Why can't I set object property to empty string in JavaScript? 在Javascript中,为什么我无法绘制我的对象的多个实例? - In Javascript, why I can't draw multiple instance of my object? 为什么在创建此 javascript object 文字时不能使用“this”? - Why can't I use 'this' when creating this javascript object literal? 为什么我不能在 Javascript 中访问这个 object 的元素? - Why can't I access the elements of this object in Javascript? 为什么我不能在JavaScript中获取导航器对象的属性数? - Why can't I get properties count of navigator object in JavaScript? 为什么我无法在javascript中访问对象属性? - Why can't I access object properties in javascript? 为什么不能附加到JSON对象? (jQuery / Java语言) - Why can't I append to my JSON object? (jQuery/Javascript) 为什么我不能在javascript中访问对象值 - why can't I acces object value in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM