Skip to content

Instantly share code, notes, and snippets.

@trungk18
Last active May 25, 2019 08:27
Embed
What would you like to do?
function generateRecursiveObj(input){
let output = {};
input.forEach(item => {
output[item.title] = {};
if(item.children && item.children.length){
output[item.title] = generateRecursiveObj(item.children);
}
})
return output;
}
var arr = [
{
title: "Section 1",
children: []
},
{
title: "Section 2",
children: [
{
title: "Section 2.1",
children: []
},
{
title: "Section 2.2",
children: []
},
{
title: "Section 2.3",
children: []
}
]
}];
generateRecursiveObj(arr);
//output
//"{
// "Section 1": {},
// "Section 2": {
// "Section 2.1": {},
// "Section 2.2": {},
// "Section 2.3": {}
// }
//}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment