Explorer
Node.js

Write a function `unflattenObject(obj)` that converts dot-notation keys back to nested objects. `{"a.b": 1}` becomes `{a: {b: 1}}`.

Problem Statement

Write a function `unflattenObject(obj)` that converts dot-notation keys back to nested objects. `{"a.b": 1}` becomes `{a: {b: 1}}`.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: Split each key by "." and build nested structure. šŸ’” Hint 2: Traverse the object creating intermediate objects as needed. šŸ’” Hint 3: For each key, walk through the path creating objects, and set the final value. āœ… Solution: ```javascript function unflattenObject(obj) { const result = {}; for (const key in obj) { const parts = key.split("."); let current = result; for (let i = 0; i < parts.length - 1; i++) { if (!current[parts[i]]) current[parts[i]] = {}; current = current[parts[i]]; } current[parts[parts.length - 1]] = obj[key]; } return result; } ```

Unflatten Object

Hard
Write a function `unflattenObject(obj)` that converts dot-notation keys back to nested objects. `{"a.b": 1}` becomes `{a: {b: 1}}`.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
{"a":{"b":1,"c":{"d":2}},"e":3}