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}});

3
  • 5
    There's no array and no json anywhere in your code. Commented Mar 8, 2018 at 10:33
  • 2
    table["xyz"] = {0:0, 1:5, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0} Commented Mar 8, 2018 at 10:35
  • 1
    You should learn some basics: Access / process (nested) objects, arrays or JSON Commented Mar 8, 2018 at 10:36

3 Answers 3

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);

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

Comments

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
  }
});

Comments

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/

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.