Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Flatten Nested Object

Data Structures, Operators & Stringshard

Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 118: Enhanced Object Literals

Implement `flattenObject(obj)` returning a flat object whose keys are dot-joined paths to each non-object value.

Sample tests

Input: flattenObject({"a":1,"b":{"c":2,"d":{"e":3}}})
Output: {"a":1,"b.c":2,"b.d.e":3}
Input: flattenObject({})
Output: {}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Recursing into arrays when they should be treated as values.
  • Forgetting the path prefix on nested keys.

Learning resources

  • MDN: Object
Approach & explanation (try first)

Depth-first recursion that joins keys with dots flattens nested objects into a single-level dotted map.

Loading...
⌘/Ctrl + Enter

Run your code to see results.