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

304
Views
How do i convert an array of objects to objects of objects?

I have an array of objects

const data = [
  { id: 1, name: "A", condition: true },
  { id: 4, name: "B", condition: false },
  { id: 7, name: "C", condition: true },
  { id: 11, name: "D", condition: true },
  { id: 12, name: "E", condition: false }
]


Sample output

const data = {
  0 : { id: 1, name: "A", condition: true },
  1 : { id: 4, name: "B", condition: false },
  2 : { id: 7, name: "C", condition: true },
  3 : { id: 11, name: "D", condition: true },
  4 : { id: 12, name: "E", condition: false }
}

I tried this way but did not get the correct keys, not sure how do i get proper keys in ascending order.

const arrayToObject = {
     return data.reduce((obj, item) => {
         obj[item['id]] = item
         return obj
     }, {})
 }

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use Object.assign

const data = [
  { id: 1, name: "A", condition: true },
  { id: 4, name: "B", condition: false },
  { id: 7, name: "C", condition: true },
  { id: 11, name: "D", condition: true },
  { id: 12, name: "E", condition: false }
]

const res = Object.assign({}, data);

console.log(res)

about 4 years ago · Juan Pablo Isaza Report

0

The reduce function accept the index as third argument, like so:

const data = [
  { id: 1, name: "A", condition: true },
  { id: 4, name: "B", condition: false },
  { id: 7, name: "C", condition: true },
  { id: 11, name: "D", condition: true },
  { id: 12, name: "E", condition: false }
]


const arrayToObject = (data) => {
     return data.reduce((obj, item, i) => {
         obj[i] = item
         return obj
     }, {})
 }

console.log(arrayToObject(data));

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!