Created
July 23, 2020 15:31
-
-
Save newpost/04f3513479922c15d335f0bd49234031 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function createNode(path, tree) { | |
const name = path.shift(); | |
const idx = tree.findIndex((e) => { | |
return e.name === name; | |
}); | |
if (idx < 0) { | |
tree.push({ | |
name: name, | |
children: [], | |
leaf: path.length === 0, | |
}); | |
if (path.length !== 0) { | |
createNode(path, tree[tree.length - 1].children); | |
} | |
} else { | |
createNode(path, tree[idx].children); | |
} | |
} | |
export default function parse(data) { | |
const tree = []; | |
for (let i = 0; i < data.length; i++) { | |
const path = data[i]; | |
const split = path.split("/"); | |
createNode(split, tree); | |
} | |
return tree; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
parse(["1/2/3", "2/3", "1/2/4"]);