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;
}
```