-2
[{"name":"Top Level","parent":"null"}]

Required output:

[{
"name": "Top Level",
"parent": "null",
"children": [
{
  "New_Key":"New_value"
}
]
}]

How can I add "New_Key":"New_value" pair under "parent" key?

Biffen
  • 6,249
  • 6
  • 28
  • 36
T Sooraj
  • 556
  • 1
  • 5
  • 18
  • 6
    `obj[0].children = [{"New_Key":"New_value"}]` – Satpal Jan 20 '17 at 06:06
  • 1
    @Sooraj T, Please use google to search. Break down your problem and find solution individually and try to merge them. – Rajesh Jan 20 '17 at 06:11
  • **First step:** - [How to add key value pair to object](http://stackoverflow.com/questions/1168807/how-can-i-add-a-key-value-pair-to-a-javascript-object). **Second Step:** Now all I need is *how to* [get object from array](http://stackoverflow.com/questions/13964155/get-javascript-object-from-array-of-objects-by-value-or-property) to add values. And Ta-Da!! – Rajesh Jan 20 '17 at 06:18

2 Answers2

-1

its very simple, you can add any new item like obj["key"] = value, in your case you can do it like below

var arrObj = [{"name":"Top Level","parent":"null"}];
for(var i=0; i<arrObj.length; i++){
        arrObj[i].children = [{"New_Key":"New_value"}]
Ranjit Singh
  • 3,715
  • 1
  • 21
  • 35
-1
var arr = [{"name":"Top Level","parent":"null"}];
for(var i=0; i<arr.length; i++){
    arr[i].children = [{
                        "New_Key":"New_value"
                      }];
}
Aravinder
  • 503
  • 4
  • 8
  • 3
    Not my downvote, but IMHO very likely because the downvoter thinks the question shouldnt *be answered*. They are expressing that we shouldnt encourage these types of questions (just answer in a comment and close the question), not saying your answer is wrong. I agree with the sentiment, but wouldnt downvote for it myself... – Wesley Smith Jan 20 '17 at 06:21
  • 4
    Not my vote, but should you not add explanation to your answer? Remember, its an answer for **readers** and not just **OP**. Also, questions that are very basic should not be answered. Since you know the solution, find relevant post and share it as a comment. This way, you'll help more people. – Rajesh Jan 20 '17 at 06:21