Skip to content

Instantly share code, notes, and snippets.

@trungvose
Last active December 12, 2023 10:10
Show Gist options
  • Save trungvose/d500694649869fdf73d7f2e6d60d4426 to your computer and use it in GitHub Desktop.
Save trungvose/d500694649869fdf73d7f2e6d60d4426 to your computer and use it in GitHub Desktop.
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