Skip to solution
mediumDSA

Flatten and Unflatten a Nested Object (dot notation & arrays)

971 views
01

Understand the problem

Create flattenObject(obj) to {"a.b.0.c": value} and unflattenObject to reverse. Support arrays and escaping.

objectflattenunflatten
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Approach: DFS to flatten, building prefix.key; for unflatten, split by . and build nested structure, detecting numeric keys for arrays.

function flattenObject(obj, prefix = '', res = {}) {
  for (const [k, v] of Object.entries(obj)) {
    const key = prefix ? `${prefix}.${k}` : k;
    if (v !== n

Solution ready — 2 min read

Classified // press E to declassify

04

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 118 of 190 decoded in the JavaScript Coding track. One more won't hurt.

Back to track