Last active
May 19, 2022 13:11
-
-
Save leonidkuznetsov18/98bc3ff92d8cc86b2ffdf477a2f516c9 to your computer and use it in GitHub Desktop.
tree to paths
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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