Description

In many frontend and backend systems, data often arrives in a flattened key-path format instead of deeply nested objects.

This commonly happens when processing:

  • form submissions
  • query parameters
  • config files
  • database patch updates

For example, APIs may return keys like "user.profile.name" instead of nested objects.

To work with this data naturally in JavaScript, we need to rebuild the original nested structure.

This challenge asks you to implement a utility that converts flattened dot-separated keys into nested objects and arrays.

Your Task

Implement a function unflatten(obj) that converts a flat object into a nested object.

It should support:

Requirement

Description

Dot notation

"a.b.c" should become nested objects

Numeric keys

"arr.0" should create arrays

Mixed nesting

Support objects inside arrays and arrays inside objects

Example

Input

const input = {
name: 'Saksham',
'skills.domain.fe.0': 'React',
'skills.domain.fe.1': 'Next.js',
'skills.domain.be.0': 'Node',
'skills.domain.be.1': 'Express',
'skills.experience': 3,
'projects.0.title': 'FitConnect',
'projects.0.tech.frontend': 'React',
'projects.0.tech.backend': 'Node'
};

Expected Output

{
name: "Saksham",
skills: {
domain: {
fe: ["React", "Next.js"],
be: ["Node", "Express"]
},
experience: 3
},
projects: [
{
title: "FitConnect",
tech: {
frontend: "React",
backend: "Node"
}
}
]
}

Constraints

  • Input keys are valid dot-separated strings
  • Numeric path segments represent array indexes
  • Input may contain mixed object and array nesting