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

135
Views
how can i convert array of objects to one object

I want to convert object1 to object2 dynamically because keys like apple and water and inside objects are not static.

const object1 = {
        apple:[
            {a:''},
            {b:''}
        ],
        water:[
            {c:''},
            {d:''}
        ]
    }

convert to this form:

object2 = {
    apple:{a:'',b:''},
    water:{c:'',d:''}
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use Object.entries to iterate the key value pairs, then use Object.assign to merge the inner objects, and finally collect the generated pairs back into one object with Object.fromEntries:

const object1 = {apple:[{a:''},{b:''}],water:[{c:''},{d:''}]}

const object2 = Object.fromEntries(
  Object.entries(object1).map(([key, arr]) =>
    [key, Object.assign({}, ...arr)]
  )
);

console.log(object2);

about 4 years ago · Juan Pablo Isaza Report

0

const object1 = {
        apple:[
            {a:''},
            {b:''}
        ],
        water:[
            {c:''},
            {d:''}
        ]
}

let object={}
Object.keys(object1).forEach((item)=>{
 let obj={};
 object1[item].map((e)=>{
    obj={...obj,...e};
 });
 object[item]=obj; 
})

console.log(object)

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!