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

105
Views
How to convert an array of objects into a single object using reduce?

I have the following array of object.

[
  { claimNumber1: 'R12345', checkNumber1: '' },
  { claimNumber2: 'T1234', checkNumber2: 'abcd' },
  { claimNumber3: 'Z4567', checkNumber3: 'qwer' }
]

Using reduce, I want to convert this to below.

{
    claimNumber1:'R12345',
    checkNumber1:'',
    claimNumber2:'T1234',
    checkNumber2:'',
    claimNumber3:'Z4567',
    checkNumber3:'',

}

I tried below but didn't get what I expected.

.reduce((obj, item) =>{
    return {...obj,item}
} ,{});
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You should spread the item object, because item is an object

const arr = [
  { claimNumber1: "R12345", checkNumber1: "" },
  { claimNumber2: "T1234", checkNumber2: "abcd" },
  { claimNumber3: "Z4567", checkNumber3: "qwer" },
];

const result = arr.reduce((obj, item, i) => {
  return { ...obj, ...item, [`checkNumber${i + 1}`]: "" };
}, {});

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Almost there. You just need to spread the item as well.

.reduce((obj, item) => {
  return {
    ...obj,
    ...item,
  };
}, {});
about 4 years ago · Juan Pablo Isaza Report

0

I think you should spread every item in reducer. Here is my code.

const res = arr.reduce((prev, item) => {
 return { ...prev, ...item };
}, {});

And result is

{
  claimNumber1: 'R12345',
  checkNumber1: '',
  claimNumber2: 'T1234',
  checkNumber2: 'abcd',
  claimNumber3: 'Z4567',
  checkNumber3: 'qwer'
}
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!