I need to convert this kind of array:
const obj = [{
name: 'firstLink',
type: 'topic',
id: 'ab75ca14-dc7c-4c3f-9115-7b1b94f88ff6',
spacing: 1, // root
}, {
name: 'secondLink',
type: 'source',
id: 'd93f154c-fb1f-4967-a70d-7d120cacfb05',
spacing: 2, // child of previous object
}, {
name: 'thirdLink',
type: 'topic',
id: '31b85921-c4af-48e5-81ae-7ce45f55df81',
spacing: 1, // root
}]
Into this object:
const map = {
'ab75ca14-dc7c-4c3f-9115-7b1b94f88ff6': {
name: 'firstLink',
type: 'topic',
children: {
'd93f154c-fb1f-4967-a70d-7d120cacfb05': {
name: 'secondLink',
type: 'source',
}
},
},
'31b85921-c4af-48e5-81ae-7ce45f55df81': {
name: 'thirdLink',
type: 'topic',
}
}
There might be up to 10 nestings, may be more (defined as spacing in the array).
How can i do that? I can use only pure js and lodash library.
spacing: 3?spacing: 2paranedId. Sou you don't need to order your array properly.. Just pointparentid: 'ab75ca14-dc7c-4c3f-9115-7b1b94f88ff6'instead ofspacing: 2