Skip to content

Instantly share code, notes, and snippets.

@YMA-MDL
Created June 9, 2021 22:48
Show Gist options
  • Save YMA-MDL/63c38021934e9d0db2e4166bf0829ff3 to your computer and use it in GitHub Desktop.
Save YMA-MDL/63c38021934e9d0db2e4166bf0829ff3 to your computer and use it in GitHub Desktop.
Test filling a table through a recursive process #js #node #javascript
const struct = [
{
a: [
{ b: [{ c: [] }, { d: [] }] },
{
e: [{ f: [] }, {
g: [
{ k: [{ l: [] }, { m: [] }] },
{ n: [{ o: [] }, { p: [] }] }
]
}]
},
{ h: [{ i: [] }, { j: [] }] }
]
}
]
const init = (struct) => {
const tableau = [];
struct.forEach(structItem => {
recursive(tableau, structItem);
});
return tableau;
}
const recursive = (tableau = [], structure) => {
tableau.push(Object.keys(structure)[0]);
structure[Object.keys(structure)[0]].forEach(structItem => {
recursive(tableau, structItem);
});
}
console.log(init(struct));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment