0

For example, I have the following JSON object json_obj1

json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}}}

Now, how could I add the following object (using javascript):

   y:{id:2,bars:{show:true,barWidth:0.4}}

to json_obj1 so that it will be:

{x:{id:1,bars:{show:true,barWidth:0.4}},y:{id:2,bars:{show:true,barWidth:0.4}}}
0

2 Answers 2

6

You can just set the field y of your json_obj1

json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}}}
json_obj1.y = {id:2,bars:{show:true,barWidth:0.4}}

Now json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}},y:{id:2,bars:{show:true,barWidth:0.4}}}

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

Comments

1

It doesn't appear as if your question actually involves JSON. The first code fragment is just a JavaScript object literal. Given your description of the problem, something like this should work:

json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}}};
json_obj1.y = {id:2,bars:{show:true,barWidth:0.4}};

This will give you the desired contents in json_obj1;

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.