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:
- The JSON object is intact
console.log(i);is showing the numbers 1-20 as expected-
When I log the
collectobject at the end, it’s structure is this:
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; it’s just overwriting
collect.i20 times and leaving me with the last value.
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.
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 variablecollect. What you want to do instead is set a property of the existingcollectobject for each value ofi, whereiis the name of the property.In order to create an object property where the name is set dynamically, you must use array-style syntax, like this:
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"] };