You can use the Spread Operator to accomplish that.

This code snippet has a function called addElements which find the target and adds the new elements to the children array.

var obj = {
  "name": "Root",
  "children": [{
      "name": "child1"
    },
    {
      "name": "child2"
    }
  ]
};


var newArray = [
   { "name": "child11"}, 
   { "name": "child12"}
];

var addElements = function(target, array) {
  obj.children.forEach(function(child) {
    if (child.name === target) {
      child['children'] = [...(child['children'] || []), ...newArray];
      return;
    }
  });
};

addElements('child1', newArray);


console.log(obj);

See? now your obj.childre[0].children array contains the new elements.

Answer from Ele on Stack Overflow
Discussions

reactjs - Adding data to a nested JSON object with children based on an array - Javascript/REACT - Stack Overflow
Hello stackoverflow community! I've been creating my own fullstack application for a while now, on the NEXTjs framework. This is going pretty well!! Unfortunately, I got stuck on a JSON import obje... More on stackoverflow.com
🌐 stackoverflow.com
node.js - NodeJS: Adding new child nodes to JSON Object - Stack Overflow
lets say there is customer object, i need to add new element address to this json object customer. how can I achieve this? Both of these are not altering the customer JSON object customer['addres... More on stackoverflow.com
🌐 stackoverflow.com
March 22, 2017
json - How to add an object to a nested javascript object using a parent id - Stack Overflow
Releases Keep up-to-date on features we add to Stack Overflow and Stack Internal. ... Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... In my application I create a JavaScript object based on a JSON response from the server similar to this: { name: "root", id: 1, children... More on stackoverflow.com
🌐 stackoverflow.com
October 15, 2012
Add new attribute (element) to JSON object using JavaScript - Stack Overflow
A JSON object is simply a javascript object, so with Javascript being a prototype based language, all you have to do is address it using the dot notation. ... @Sunil Garg How would you store that value as a child to some parent in the original object? More on stackoverflow.com
🌐 stackoverflow.com
🌐
Stack Overflow
stackoverflow.com › questions › 74460510 › adding-data-to-a-nested-json-object-with-children-based-on-an-array-javascript
reactjs - Adding data to a nested JSON object with children based on an array - Javascript/REACT - Stack Overflow
1 Calculated a value in child of nested json and add it as a new elements to the original nested json ... 0 Loop through array of objects and assign parent object to each child array of object using javascript
🌐
Stack Overflow
stackoverflow.com › questions › 42947202 › nodejs-adding-new-child-nodes-to-json-object
node.js - NodeJS: Adding new child nodes to JSON Object - Stack Overflow
March 22, 2017 - and I can not use push() as this is not adding a new item in list of objects. ... Maybe your addressObj is not properly formed. ... var customer = {"name": "Naren"}; customer.address1 = "stackoverflow"; customer.address2 = {"fulladdress":"stackoverflow"}; JSON.stringify(customer)
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-how-to-add-an-element-to-a-json-object
How to Add an Element to a JSON Object using JavaScript? | GeeksforGeeks
August 27, 2024 - In JavaScript to add an element to a JSON object by simply assigning a value to a new key.
🌐
Pluralsight
pluralsight.com › tech insights & how-to guides › tech guides & tutorials
How to Pass a JSON Object from Child Component to Parent Component in React | Pluralsight
With state and props, you can pass several data types, including string, array, number, and object. In this guide, you will learn how to pass data from a parent component to a child component and vice versa using different approaches including using props and state.
🌐
Reddit
reddit.com › r/learnjavascript › need help putting objects under parent objects in a json file
r/learnjavascript on Reddit: Need help putting objects under parent objects in a JSON file
November 24, 2022 -

Hi all,

We have a comment JSON file that looks like this:

{
    "comment": "This is a comment.",
    "parent_id": 0,
    "name": "Kelvin",
    "date": 2014-06-09,
    "id": 118751,
  },
  {
    "comment": "This is a reply.",
    "parent_id": 118751,
    "name": "Natalie",
    "date": 2014-05-22,
    "id": 111247,
  },
    {
    "comment": "This is another reply.",
    "parent_id": 118751,
    "name": "John",
    "date": 2014-05-22,
    "id": 111347,
  }
{
    "comment": "This is another parent comment.",
    "parent_id": 0,
    "name": "Steven",
    "date": 2014-06-09,
    "id": 118752,
  },
  {
    "comment": "This is a reply to Steven's comment.",
    "parent_id": 118752,
    "name": "Natalie",
    "date": 2014-05-22,
    "id": 111247,
  },
    {
    "comment": "This is another reply to Steven's comment.",
    "parent_id": 118752,
    "name": "John",
    "date": 2014-05-22,
    "id": 111347,
  }

We map over this to list out the comments on our blog, desc by date.

Here's the issue:

We need to restructure this so that child comments are somehow under their parents:

parent:
{
    "comment": "This is a comment.",
    "parent_id": 0,
    "name": "Kelvin",
    "date": 2014-06-09,
    "id": 118751,
  },
children: [
  {
    "comment": "This is a reply.",
    "parent_id": 118751,
    "name": "Natalie",
    "date": 2014-05-22,
    "id": 111247,
  },
    {
    "comment": "This is another reply.",
    "parent_id": 118751,
    "name": "John",
    "date": 2014-05-22,
    "id": 111347,
  }
]
parent:
{
    "comment": "This is another parent comment.",
    "parent_id": 0,
    "name": "Steven",
    "date": 2014-06-09,
    "id": 118752,
  },
children: [
  {
    "comment": "This is a reply to Steven's comment.",
    "parent_id": 118752,
    "name": "Natalie",
    "date": 2014-05-22,
    "id": 111247,
  },
    {
    "comment": "This is another reply to Steven's comment.",
    "parent_id": 118752,
    "name": "John",
    "date": 2014-05-22,
    "id": 111347,
  }
]

And so on.

So basically we'd map over it to get the parent comments, and then map over it again to get the children of those parent comments, and then somehow combine it all together.

Is there an easy way to do this? Stumped. (this is a React/NextJS site)

You can see that the first/parent comment has parent_id of 0, and then child comments have parent_id that matches the id of the parent.

Much appreciated.

🌐
freeCodeCamp
forum.freecodecamp.org › t › json-how-to-insert-url-in-the-child-node › 340027
JSON: How to insert URL in the child node? - The freeCodeCamp Forum
January 10, 2020 - I have question with two options. If user select option 1 then I would like to redirect to URL1 and if user select option 2 then I would like to redirect to URL2. How can I do that? Please see attached screenshot. JSO…
🌐
Coderanch
coderanch.com › t › 735043 › languages › add-child-node-parent-node
How to add the child node to the corresponding parent node in JSON? (Other Languages forum at Coderanch)
September 29, 2020 - So eventually, I get something like that: The thing I try to achieve is, I want to make all these structures as a single JS object: The idea is I have at the moment 2 arrays. One for the first level of items and the second array is the child node containing the files to each parent folder. How can combine these two arrays so I build the structure shown above? The main developer of this project had used Lodash for that. Is there a way to accomplish that? Or it can be done with pure Javascript?
🌐
SitePoint
sitepoint.com › javascript
Adding items to a JSON object - JavaScript - SitePoint Forums | Web Development & Design Community
March 3, 2009 - Does anyone know the function for adding more items to an already declared JSON object · Not correct!! The line event[i] is wrong. When i send ‘events’ to console i get something this - iterating over a numer of variables and adding their values to the JSON object: · I reckon the object ...