{
name: "one"
children: [
{ name: "two",
children[{
name: "three",
children[]}]
},
{ name: "four",
children[{
name: "five",
children[]}]
}
]
}
{
one: [{listOfChildren}],
two: [{listOfChildren}]
three:[{listOfChildren}]
}
function toFlatMap(array, map = {}) {
for(const {name, children} of array) {
map[name] = children;
toFlatMap(children, map);
}
return map;
}
function toFlatMap(array, map = {}) {
let stack = array.slice();
let current;
while(current = stack.shift()) {
map[current.name] = current.children;
stack.push(...current.children);
}
return map;
}
let menu = {}
function getMenu(obj){
if(obj.children.length > 0){
menu[obj.url] = obj.children
}
obj.children.map(i => getMenu(i))
return menu;
}