Skip to content
Advertisement

Create nested JSX list items from multi-level nested array of objects reactjs

I am trying to create a nested JSX list items from nested object array. Below is the array:

JavaScript

Below is what the array should be converted to:

JavaScript

Here is my approach:

JavaScript

React does not allow adding individual markups into array. Please help provide a solution.
P.S.: I apologize in advance if you find something wrong with formatting. This is the first time I am posting on stackOverflow.

Advertisement

Answer

You can “render” your children into a variable and use this directly in your component. The trick then is to use a recursive component. That way it doesn‘t matter how deep your tree is. You don‘t need to edit this component if your tree gets deeper.

Here is how that might look like:

JavaScript

Here is a working example on Codesandbox with your data.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement