1

I have an object below. I need to write a for loop which iterates every children and display ID.

I used underscore library but didn't found solution

   *[  
   {  
      Id:1,
      Name:"Test",
      children:[  
         {  
            Id:11,
            Name:"Test",
            children:[  
               {  
                  Id:113,
                  Name:"Test",
                  children:[  

                  ]
               },
               {  
                  Id:114,
                  Name:"Test",
                  children:[  

                  ]
               }
            ]
         },
         {  
            Id:12,
            Name:"Test",
            children:[  

            ]
         },
         {  
            Id:13,
            Name:"Test",
            children:[  
               {  
                  Id:115,
                  Name:"Test",
                  children:[  
                     {  
                        Id:1111,
                        Name:"Test",
                        children:[  

                        ]
                     }
                  ]
               }
            ]
         },
         {  
            Id:14,
            Name:"Test",
            children:[  

            ]
         }
      ]
   }   {  
      Id:2,
      Name:"Test",
      children:[  

      ]
   }
]*
3
  • Please show the actual code which you tried. We can help you fix that. Commented Jan 17, 2018 at 6:30
  • What is the expected result? Commented Jan 17, 2018 at 6:31
  • need to display each ID from the above object Commented Jan 17, 2018 at 6:32

3 Answers 3

4

You can do it resursively:

var items= [  
   {  
      Id:1,
      Name:"Test",
      children:[  
         {  
            Id:11,
            Name:"Test",
            children:[  
               {  
                  Id:113,
                  Name:"Test",
                  children:[  

                  ]
               },
               {  
                  Id:114,
                  Name:"Test",
                  children:[  

                  ]
               }
            ]
         },
         {  
            Id:12,
            Name:"Test",
            children:[  

            ]
         },
         {  
            Id:13,
            Name:"Test",
            children:[  
               {  
                  Id:115,
                  Name:"Test",
                  children:[  
                     {  
                        Id:1111,
                        Name:"Test",
                        children:[  

                        ]
                     }
                  ]
               }
            ]
         },
         {  
            Id:14,
            Name:"Test",
            children:[  

            ]
         }
      ]
   },   {  
      Id:2,
      Name:"Test",
      children:[  

      ]
   }
];

displayIds(items);

function displayIds(datas) {
  datas.forEach(function(data) {
    console.log(data.Id);
    if (data.children) {
      displayIds(data.children);
    }
  });
}

Sign up to request clarification or add additional context in comments.

2 Comments

I need code in JS 5. Currently my application works in ECMA 5
could you please tell how to find parent object. i.e. if I have 115 I need to know the parent of it
1

var data = [  
   {  
      Id:1,
      Name:"Test",
      children:[  
         {  
            Id:11,
            Name:"Test",
            children:[  
               {  
                  Id:113,
                  Name:"Test",
                  children:[  

                  ]
               },
               {  
                  Id:114,
                  Name:"Test",
                  children:[  

                  ]
               }
            ]
         },
         {  
            Id:12,
            Name:"Test",
            children:[  

            ]
         },
         {  
            Id:13,
            Name:"Test",
            children:[  
               {  
                  Id:115,
                  Name:"Test",
                  children:[  
                     {  
                        Id:1111,
                        Name:"Test",
                        children:[  

                        ]
                     }
                  ]
               }
            ]
         },
         {  
            Id:14,
            Name:"Test",
            children:[  

            ]
         }
      ]
   },
   {  
      Id:2,
      Name:"Test",
      children:[  

      ]
   }
];

showId(data);

function showId(data){
  for(var i = 0; i < data.length; i++){
  console.log(data[i].Id);
  if(data[i].children.length > 0){
    showId(data[i].children);
  }
}
}

Comments

0

You can use recursive for this like

var obj = [  
   {  
      Id:1,
      Name:"Test",
      children:[  
         {  
            Id:11,
            Name:"Test",
            children:[  
               {  
                  Id:113,
                  Name:"Test",
                  children:[  

                  ]
               },
               {  
                  Id:114,
                  Name:"Test",
                  children:[  

                  ]
               }
            ]
         },
         {  
            Id:12,
            Name:"Test",
            children:[  

            ]
         },
         {  
            Id:13,
            Name:"Test",
            children:[  
               {  
                  Id:115,
                  Name:"Test",
                  children:[  
                     {  
                        Id:1111,
                        Name:"Test",
                        children:[  

                        ]
                     }
                  ]
               }
            ]
         },
         {  
            Id:14,
            Name:"Test",
            children:[  

            ]
         }
      ]
   },   {  
      Id:2,
      Name:"Test",
      children:[  

      ]
   }
]

function showIds(obj, i=0) {
  if(!obj[i]) return
  console.log(obj[i].Id);
  showIds(obj, i+1)
  if(obj[i].children) showIds(obj[i].children);
}

showIds(obj)
  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.