I've been building upon a previous post
building a menu list object recursively in javascript
I used that to learn more about arrays and objects in js and it output
{
social: {
swipes: {
women: null
}
}
}
but now I want it in a way that's more practical that'd make it easier to traverse in a format like
{
social: {
children: {
swipes: {
children: {
women: null
}
}
}
}
}
with a children key.
how can I modify the code to do so?
let input = ['/social/swipes/women', '/social/swipes/men', '/upgrade/premium'];
let output = input.reduce((o, e) => {
let z = e.split("/").filter(d => d);
z.reduce((k,v,i) => {
if (z.length - 1 !== i) {
if (k.hasOwnProperty(v)) {
k[v] = k[v];
} else {
k[v] = { children: {}};
}
} else {
k[v] = null;
}
return k[v]
}, o)
return o;
}, {})
console.log(output);