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

94
Views
How to conver list array to object?

How to convert array as below to object like output below?

const list = [
  { id: 1, data: { name: 'John', age: 25 } },
  { id: 2, data: { name: 'Jane', age: 22 } },
  { id: 3, data: { name: 'Tom', age: 20 } }
]

output:

{
  1: { id: 1, name: 'John', age: 25 },
  2: { id: 2, name: 'Jane', age: 22 },
  3: { id: 3, name: 'Tom', age: 20 }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use array reduce method. Inside the callback method add the key in the accumulator object represented by acc in the below code

const list = [{
    id: 1,
    data: {
      name: 'John',
      age: 25
    }
  },
  {
    id: 2,
    data: {
      name: 'Jane',
      age: 22
    }
  },
  {
    id: 3,
    data: {
      name: 'Tom',
      age: 20
    }
  }
]

const newData = list.reduce((acc, curr) => {
  acc[curr.id] = { ...curr.data,
    id: curr.id
  }
  return acc;
}, {});
console.log(newData)

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!