1

I want the following structure:

    var data={"users":[
    {
        "firstName":"Ray",
        "lastName":"Villalobos",
        "joined":2012
    },
    {
        "firstName":"John",
        "lastName":"Jones",
        "joined":2010
    }
    ]}

I want to be able to programatically add entries to it. Here is what I tried:

  var data = [];
   data.push({
     "firstName":"Johsssn",
     "lastName":"Jossnes",
     "joined":2010
   });
2
  • And what have you got trying? Error? Commented Jun 26, 2013 at 16:22
  • yep,jcsanyi's solution solved it. Commented Jun 26, 2013 at 16:57

3 Answers 3

7

Your array is actually data.users, not data.
So use this instead:

var data = {users: []};
data.users.push({
  "firstName":"Johsssn",
  "lastName":"Jossnes",
  "joined":2010
});
Sign up to request clarification or add additional context in comments.

1 Comment

This is how I'd do it. Also, you can use bracket notation (data["users"]) instead of dot notation (data.users).
0
    var data = {users: []};
    data.users.push({
         "firstName":"Johsssn",
         "lastName":"Jossnes",
         "joined":2010
    });

Comments

0

Your data object contains one property, users, which is an array of users, so you need to push into it:

data.users.push({
    "firstName":"Johsssn",
    "lastName":"Jossnes",
     "joined":2010
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.