How to convert a flat array of objects into a nested JSON data in JavaScript/React using split() method?


I have a flat array of objects which is quite complex and there are no ids in the objects. I have to convert this flat array into nested JSON data by splitting the second element’s value and each value will be children"data": [ { "asset": "332", "func": "5928-LBV-2301", "ftext": "LBV" }, { "asset": "333", "func": "5928-LBV-2301", "ftext": "LBV" }, { "asset": "336", "func": "5928-LBV-2302", "ftext": "LBV" }, { "asset": "340", "func": "5928-LBV-2302", "ftext": "LBV" }, { "asset": "332", "func": "5928-LBV-2302", "ftext": "LBV" },

of the other.
My expected output would be:
5928-
LBV-
2301-
332
333
5928-
LBV-
2302-
336

and so on. The third level which is 2301,2302 etc will have the asset value as its child. Any idea how to achieve this? I am fairly new to React. Please help!

Source: JavaSript – Stack Overflow

September 9, 2021
Category : News
Tags: arrays | javascript | json | reactjs | treeview

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.