Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 666109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:46:57+00:00 2026-05-13T23:46:57+00:00

I have an object I’m trying to populate from another object (that is, iterate

  • 0

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 collect object 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.i 20 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!

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:46:57+00:00Added an answer on May 13, 2026 at 11:46 pm

    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. 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.

    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"] };

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I read somewhere that some modules only have object oriented interfaces (
I have an object that has pairs of replacement values used for simple encoding
I have two tables: object that has object_id column and avalues that have object_id
I have shared object A.so which statically links to libssl.a & another shared object
I have an object that cannot be copied, a NetGame because it has a
We have Object and ValueType .... And String .... String is derived from object,
Let's imagine that we have object Animal $.Animal = function(options) { this.defaults = {
I have object A and object B deserialized from binary files. B has a
I have many large gzip files (approximately 10MB - 200MB) that I downloaded from
I have object that is serialized into db. The question is where to keep

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.