Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a (key : value) into a infinite nested children object

I have an Array like below (code session 2) . And I want add id : random into {child Obj} like this:

{
  name : "Peter",
  age : "18",
  profession : "nurse",
  id : String(Math.random())
  children : [],
 }

But it have 2 cases : value of key children may be [ ] or [ length !== 0 ]. I want to loop infinity the parent Array and add id for all.

Note : value of key children inside every {childObj} maybe an Array look like Parent Array. My final target is set id for all element from response API look like the template array

Thank you too much and sorry about my English if it make you feel complex

[{
  name : "Peter",
  age : "18",
  profession : "nurse",
  children : []
 }
 { 
   name: "Jack",
   age: "98" ,
   profession: "doctor",
   children: [ {
                 name : "Peter",
                 age : "18",
                 profession : "nurse",
                children : []
               },
               {
                 name: "Varun",
                 age: "80"
                 profession: "scientist"
                 children: [
                             {
                                name: "Ishan"
                                age: "62",
                                profession: "teacher
                                children: [{....
                                 .....
                                 .....[{
                                       name: "Rahul",
                                       age: "23",
                                       profession: "engineer"
                                      children: [{
                                                  .....
like image 251
NguyenTam2206 Avatar asked Jun 08 '20 11:06

NguyenTam2206


People also ask

How I add key value to the all objects in array in JavaScript?

To add a key/value pair to all objects in an array:Use the Array. forEach() method to iterate over the array. On each iteration, use dot notation to add a key/value pair to the current object. The key/value pair will get added to all objects in the array.

How do you get keys for nested objects?

You can access the nested object using indexes 0, 1, 2, etc. So data[0]. alt[0] will access the first alt nested object. Now you can call the above mentioned keys() function to get a list of its keys.

How do you store key value pairs in objects?

How to Add a Key-Value Pair Using Bracket Notation in JavaScript. Just like dot notation, you can use bracket notation to add a key-value pair to an object. Bracket notation offers more flexibility than dot notation. That's because key names can include spaces and hyphens, and they can start with numbers.


1 Answers

You can use .map() on your original data array, which returns the object along with its id. The children in the mapped object will be a mapped array, again using the same mapping function like so:

const data = [{ name: "Peter", age: "18", profession: "nurse", children: [] }, { name: "Jack", age: "98", profession: "doctor", children: [{ name: "Peter", age: "18", profession: "nurse", children: [] }, { name: "Varun", age: "80", profession: "scientist", children: [{ name: "Ishan", age: "62", profession: "teacher", children: [{ name: "Rahul", age: "23", profession: "engineer", children: [] }] }] } ] } ];

const res = data.map(function mapId({children, ...r}) {
  return {...r, id: String(Math.random()), children: children.map(mapId)};
});
console.log(res);
like image 138
Nick Parsons Avatar answered Oct 12 '22 04:10

Nick Parsons