This is the given array:
array = [{number : 1 , name : 'one' , child : [] },{number : 2, name : 'two' , child : []},{number : 3 , name : 'three' , child : []}]
How to create nested child objects in JavaScript from this array?
nested = [
{
number : 1,
name : 'one',
child : [
{
number : 2,
name : 'two',
child : [{
number : 3,
name : 'three',
child : []
}
]
}
]
}
]
i try to solve this but i cant do this
2
Answers
You can use the
array.reduceRight
method to achieve your result.Here’s how you can do it:
You can use recursion to solve this problem.
Here’s how you can do it:
I hope this answer can help you even a little.