2

I want the following structure:

    {
     "1":[
        {"name":"John","Age":"21"}
      ]
     "2":[
        {"name":"Jone","Age":"22"}
      ]
    }

I want to dynamically add objects.This is what I tried:

    var i = 0;
    var data= [{i:{}}]; 
    function add(){
      data= [{i:{}}];
      data.i.push({
        "name":"Zack",
        "age":22
      });
      i++;
    }

I got an error "Cannot call method 'push' of undefined"

3 Answers 3

4
  1. To access a property dynamically, use the bracket notation. data= [{i:{}}] doesn't do what you want, it doesn't use the fact you just defined the i variable.

  2. In your function, you're replacing the external value of data.

What you want is probably much simpler :

var i = 0;
var data= {};
function add(){
  data[i] = data[i]||[];
  data[i].push({
    "name":"Zack",
    "age":22
  });
  i++;
}

More details in this MDN documentation : Working with objects

Sign up to request clarification or add additional context in comments.

7 Comments

Looks like he wrapped his object in an array. data[0].i?
data[i++].push ... one less line :)
var data={}; worked!But like Raibaz said,data[i] is not an array so .push didnt work.Using data[i] = {"name": "Raibaz", "age": 30} worked.
@phpcoderx using directly data[i] = your object doesn't build the same structure than the one you showed. Note that I updated the code with data[i] = data[i]||[] to ensure data[i] is an array and is initialized.
Sorry,didn't see your edit.And btw is there a way to read the name using i as index? I tried alert(data[i].name); .But it gave me an error"Cannot read property 'name' of undefined "
|
0

By declaring data as [{i:{}}], the structure you are obtaining is actually

[
    {
        i: {
        }
    }
]

Which is not the one you wanted, and the reason why you can't access data.i (since data is an array).

You should declare data as {i:{}} and then access i with the bracket notation, as data[i].

Besides, since data[i] is not an array, .push won't work either, so you should use something like data[i] = {"name": "Raibaz", "age": 30}.

Comments

0

In Javascript multi dimensional arrays are created by using following syntax

var mArray = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]];

and

mArray[0][0] gives 1
mArray[0][1] gives 2
mArray[1][0] gives 3
mArray[1][1] gives 4

You can initialize your multidimensional array as given below

var i = 0;
var data= []; 

What you are trying to create is an object of arrays. In that case syntax is (as in your code)

var mObject = {
 "1":[
    {"name":"John","Age":"21"}
  ]
 "2":[
    {"name":"Jone","Age":"22"}
  ]
};

You can initialize your object as given below

var i = 1;
var data= {}; 

And in both cases the function can be written as given below

function add(){
  data[i] = data[i] || []; // initializes array if it is undefined
  data[i].push({
    "name":"Zack",
    "age":22
  });
  i++;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.