Unflatten Dotted Keys
Write the code, see it run, pass the checks.
The problem
- medium
- Code
- Pro
Write unflattenObject(flat), the reverse of flattening. flat is an object whose keys are paths with dots between the parts, such as user.tags.0. Return a new nested object with each value placed at its path. When a part of a path needs a new container, make it an array if the part after it is a whole number (digits only) and a plain object otherwise, so the keys tags.0 and tags.1 build an array called tags. Keys without dots stay at the top level, and every value is placed as it is, including empty objects and arrays. Paths never conflict: you'll never get both a and a.b. Leave flat unchanged.
Your code must pass
- Builds nested objects from dotted keys
- Builds arrays for whole-number parts
- Keeps keys without dots at the top level
- Places values as they are, including empty objects and arrays
- Leaves the flat object unchanged