Skip to content

Instantly share code, notes, and snippets.

@leonidkuznetsov18
Last active May 19, 2022 13:11
Show Gist options
  • Save leonidkuznetsov18/98bc3ff92d8cc86b2ffdf477a2f516c9 to your computer and use it in GitHub Desktop.
Save leonidkuznetsov18/98bc3ff92d8cc86b2ffdf477a2f516c9 to your computer and use it in GitHub Desktop.
tree to paths
const data = [
{
"name": "path",
"children": [
{
"name": "to",
"children": [
{
"name": "file1",
"value": true,
},
{
"name": "file2",
"value": false,
}
]
}
]
},
{
"name": "foo",
"children": [
{
"name": "bar",
"value": false,
}
]
}
]
function getNames(data) {
var result = {};
function loop(data, c) {
data.forEach(function (e) {
const name = !c.length ? e.name : c + '/' + e.name;
if (e.children) {
loop(e.children, name);
}
else {
result[name] = e.value;
}
});
}
loop(data, '');
return result
}
console.log(getNames(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment