0

var table = {"abc":{0:0, 1:5, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0}};

This is my JSON array, I can access the data like this now: table["abc"][1]

Now, How do I append another element like abc Something like this:

table.append({"xyz":{0:0, 1:5, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0}});

Rahul
  • 15
  • 2

3 Answers3

1

Similar with the way you access it

var table = {"abc":{0:0, 1:5, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0}};

console.log(table["abc"][1]);

table["xyz"] = {0:0, 1:5, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0};

console.log(table);
Thum Choon Tat
  • 3,084
  • 1
  • 22
  • 24
0

The immutable way:

var table = {"abc":{0:0, 1:5, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0}};

table = {
   ...table,
   xyz: {
     0:0, 
     1:5, 
     2:0, 
     3:0, 
     4:0, 
     5:0, 
     6:0, 
     7:0, 
     8:0
   }
}

Not got Object Spread support? Try this:

table = Object.assign({}, table, {
  xyz: {
    0:0, 
    1:5, 
    2:0, 
    3:0, 
    4:0, 
    5:0, 
    6:0, 
    7:0, 
    8:0
  }
});
Dan
  • 8,041
  • 8
  • 41
  • 72
0

In this way you can add more data to table object

table.xyz = {0:0, 1:5, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0}

its just as simple. Your new table will be as

var table = {"abc":{0:0, 1:5, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0},
         "xyz":{0:0, 1:5, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0}};

Here is jsfiddle link https://jsfiddle.net/mustkeom/pykmd60j/

Mustkeem K
  • 8,158
  • 2
  • 32
  • 43