Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

95
Views
Creating a nested object from entries

Is there a way to generate a nested JavaScript Object from entries?

Object.fromEntries() doesn't quite do it since it doesn't do nested objects.

const entries = [['a.b', 'c'], ['a.d', 'e']]

// Object.fromEntries(entries) returns:
{
    'a.b': 'c',
    'a.d': 'e',
}

// whatIAmLookingFor(entries) returns:
{
    a: {
        b: 'c',
        d: 'e',
    }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could reduce the array entries and reduce the keys as well. Then assign the value to the final object with the last key.

const
    setValue = (object, [key, value]) => {
        const
            keys = key.split('.'),
            last = keys.pop();
        keys.reduce((o, k) => o[k] ??= {}, object)[last] = value;
        return object;
    },
    entries = [['a.b', 'c'], ['a.d', 'e']],
    result = entries.reduce(setValue, {});

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

I think I found a way using lodash:

import set from 'lodash/set'

const result = {}
const entries = [['a.b', 'c'], ['a.d', 'e']]

entries.forEach((entry) => {
    const key = entry[0]
    const value = entry[1]
    set(result, key, value)
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!