I need to create a dynamic ul \ li nested list from a json array.
NOTE! I can do this conversion myself using jQuery, but in this case I need to work with the string since node.js and I do not have access to the DOM.
Also, the array can have different depths.
This is the json data I'm working with, and how it should look after conversion.
var data = [{"id": "1", "name": "name_1", "parent_id": "0", "depth": "0"}, {"id": "2", "name": "name_2", "parent_id": "0", "depth": "0"}, {"id": "3", "name": "name_3", "parent_id": "1", "depth": "1"}, {"id": "4", "name": "name_4", "parent_id": "3", "depth": "2"}]; <ul> <li>name_1</li> //depth 0 <li>name_2 //depth 0 <ul> <li>name_3 //depth 1 <ul> <li>name_3</li> //depth 2 </ul> </li> </ul> </li> </ul>
Hope this is clear. If not, please ask any questions in the comment. Thank you for your promotion.
user2960708
source share