Resolve Promises Sequentially
Run promises one after another and collect the results in order — built with .then chaining, no async/await.
JavaScript · ES6 — Pratik Rai ·
Turn a flat array of nodes with parentId into a nested tree — the shape behind every file explorer and nested menu.
Given a flat list of { id, name, parentId }, write buildTree(items) returning the roots of a tree.
Every node carries a children array. parentId is null for a root. Roots come back in the order they appeared. A child may be listed before its parent.
Input:
JSfile.javascript1buildTree([ 2 { id: 1, name: 'Parent', parentId: null }, 3 { id: 2, name: 'Child', parentId: 1 }, 4]);
Output:
[{ id: 1, name: 'Parent', parentId: null,
children: [{ id: 2, name: 'Child', parentId: 1, children: [] }] }]
The child is nested under the node whose id its parentId names.
Goal: Produce the roots of a correctly nested tree in one pass over a lookup.
Continue learning with these related challenges
Run promises one after another and collect the results in order — built with .then chaining, no async/await.
JavaScript · ES6 — Pratik Rai ·
Compare two values structurally, because === only ever compares references.
JavaScript · ES6 — Pratik Rai ·
Copy a nested structure so nothing is shared — including the cases that break a naive recursion.
JavaScript · ES6 — Pratik Rai ·
Run promises one after another and collect the results in order — built with .then chaining, no async/await.
JavaScript · ES6
Pratik Rai ·
Compare two values structurally, because === only ever compares references.
JavaScript · ES6
Pratik Rai ·
Copy a nested structure so nothing is shared — including the cases that break a naive recursion.
JavaScript · ES6
Pratik Rai ·