Skip to content
Advertisement

How to return grandchilds names for each person in array JS?

The function should return the name of grandchildrens for a given id and store the result in an array.The process should continue till the last level of granchild. If the given Id does not have any child return empty array. I have made the code below:

JavaScript

Advertisement

Answer

I guess I understood what you need and to me looks like you are going on the right way but since JS allows you create dictionaries very easily and this is create tool for indexing your information… in your problem I would index the people array by their parentId. Also… by your description you have to add them to an array up to the last grandchildren…

So…

JavaScript
Advertisement