skip to Main Content

Restructure a javascript Array of Objects for Ecommerce categories and subcategories

Input let categories =[ {id:1, name:'Electronics', parentId:''}, {id:2, name:'Devices', parentId:1}, {id:3, name:'Mobile Phones', parentId:2}, {id:4, name:'TV', parentId:2}, {id:5, name:'Accessories', parentId:1}, {id:6, name:'Beauty', parentId:''}, ] Expected Output let categories =[ {id:1, name:'Electronics', parentId:'', subCategories:[ {id:2, name:'Devices', parentId:1, subCategories:[ {id:3, name:'Mobile Phones',…

VIEW QUESTION
Back To Top
Search