Created
June 9, 2021 22:48
-
-
Save YMA-MDL/63c38021934e9d0db2e4166bf0829ff3 to your computer and use it in GitHub Desktop.
Test filling a table through a recursive process #js #node #javascript
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
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