Skip to content

Instantly share code, notes, and snippets.

View ThermoDev's full-sized avatar
😓

Michael Pegios ThermoDev

😓
View GitHub Profile
@ThermoDev
ThermoDev / maketree.js
Created May 14, 2017 12:58 — forked from mpj/maketree.js
Maketree for recursion episode, more functional and with better time complexity
const createChildLookupTable = categories =>
categories.reduce((lookup, child) =>
Object.assign({}, lookup, {
[child.parent]: (lookup[child.parent] || []).concat(child)
}), {})
const makeTree = (childLookup, parent) =>
(childLookup[parent] || []).reduce((node, child) =>
Object.assign({}, node, {
[child.id]: makeTree(childLookup, child.id)