java script filling up an array so it matches following structure

113 Views Asked by At

lets say i want to start with empty value of variable data, how can to achieve this result with javascript using push method:

var data = [
 {
      label: 'node1',
      children: [
      { label: 'child1' },
      { label: 'child2' }
        ]
      },
      {
      label: 'node2',
      children: [
      { label: 'child3' }
      ]
      }
 ]; 

i have tried:

data.push("label: nodel", "children:"+ ['child1', 'child2']);

looking at code above i need to insert one element that will be linked with list of childs. Can someone help me achieve this.. i would be very grateful. Best regards.

1

There are 1 best solutions below

0
On BEST ANSWER

Is this what you mean?

var object1 = {
                label: 'node1',
                children: [
                    { label: 'child1' },
                    { label: 'child2' }
                    ]
                };
var data = new Array();
data.push(object1);

OR

data.push({ label: 'node1', children: [ { label: 'child1' }, { label: 'child2' } ] });

EDITED TO SHOW YOSHIS VERSION ASWELL