最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add a (key : value) into a infinite nested children object - Stack Overflow

programmeradmin5浏览0评论

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 plex

[{
  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: [{
                                                  .....

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 plex

[{
  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: [{
                                                  .....
Share Improve this question edited Jun 8, 2020 at 11:44 Eduardo Fellipe 4184 silver badges14 bronze badges asked Jun 8, 2020 at 11:17 NguyenTam2206NguyenTam2206 613 bronze badges 2
  • As I understand it you want to go through the array and add an id for each object (parent / child), correct? – Aks Jacoves Commented Jun 8, 2020 at 11:24
  • Yes sir, but inside a child have a key that maybe has an array like big array. It maybe infinity – NguyenTam2206 Commented Jun 8, 2020 at 12:51
Add a ment  | 

3 Answers 3

Reset to default 7

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);

A good use case case of recursion:

var data = [{
  name : "Peter",
  age : "18",
  profession : "nurse",
  children : []
 },
 { 
   name: "Jack",
   age: "98" ,
   profession: "doctor",
   children: [ {
                 name : "Peter",
                 age : "18",
                 profession : "nurse",
                children : []
               },]
}
]
function addIdRecursively(data){
   data.forEach(i=> {
       i.id = String(Math.floor(Math.random()*10));
       if(i.children.length>0){
         addIdRecursively(i.children)
       }
  })
}
addIdRecursively(data);

console.log(data)

const obj = [{
  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: [],
        },
      ],
    },
  ],
}];

const f = (_) => ({..._, id: String(Math.random()), children: _.children.map(f) })

console.log(obj.map(f));
发布评论

评论列表(0)

  1. 暂无评论