I have declared a JSON Object and added some key value pair in that like:
var obj = {};
and added some data into it like:
obj = { "1":"aa", "2":"bb" };
But I want to add more key value pair in the same object, if I add key value pair same above mentioned then it replace the old one. So could any one please tell me how I can append data in the same JSON Object i.e. obj.
A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma.
Could you do the following:
obj = { "1":"aa", "2":"bb" }; var newNum = "3"; var newVal = "cc"; obj[newNum] = newVal; alert(obj["3"]); // this would alert 'cc'
You can use dot notation or bracket notation ...
var obj = {}; obj = { "1": "aa", "2": "bb" }; obj.another = "valuehere"; obj["3"] = "cc";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With