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

177
Views
Use of Object.entries()

Is there a way to simplify this code with the use of Object.entries()? I want to remove new Map().

const err = [{ 
 'id': 1, 
 'error': ["Error 1", "Error2"]
}]

const warn = [{ 
 'id': 1, 
 'warning': ["Warn 1", "Warn 2"]
}]

const map = new Map();
err.forEach(item=> map.set(item.id, item));
warn.forEach(item=> map.set(item.id, {...map.get(item.id), ...item}));
const combined = Array.from(map.values());
console.log(combined)

Tried:

const map = new Map(Object.entries(err));
warn.forEach(item=> map.set(item.id, {...map.get(item.id), ...item}));
const combined = Array.from(map.values());
console.log(combined)

The output should still be the same

[{ 
 'id': 1, 
 'error': ["Error 1", "Error2"],
 'warning': ["Warn 1", "Warn 2"] 
}]
about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

You can use Array.prototype.map() to create the key/value pairs for the new Map() argument.

const err = [{
  'id': 1,
  'error': ["Error 1", "Error 2"]
}]

const warn = [{
  'id': 1,
  'warning': ["Warn 1", "Warn 2"]
}]


const map = new Map(err.map(el => [el.id, el]));
warn.forEach(el => map.get(el.id).warning = el.warning);
const combined = Array.from(map.values());
console.log(combined)
Object.entries() isn't useful because the keys are the array indexes, not the id properties.

about 4 years ago · Santiago Gelvez Report

0

If you expect more than 1 item per array you can do:

const err = [{ 
 'id': 1, 
 'error': ["Error 1", "Error2"]
}]

const warn = [{ 
 'id': 1, 
 'warning': ["Warn 1", "Warn 2"]
}]


const newObj = err.map( (item,i) => Object.assign({},item,warn[i]));

console.log(newObj)

If the final object depends on those other arrays and you know in advance that they have exactly length 1 then is simpler:

const err = [{ 
 'id': 1, 
 'error': ["Error 1", "Error2"]
}]

const warn = [{ 
 'id': 1, 
 'warning': ["Warn 1", "Warn 2"]
}]


const newArr = [Object.assign({},err[0],warn[0])]

console.log(newArr)

about 4 years ago · Santiago Gelvez Report

0

Object entries is for object, not an array. However you can group by using reduce into object, then get his values to turn into array.

const err = [{
  'id': 1,
  'error': ["Error 1", "Error 2"]
}]

const warn = [{
  'id': 1,
  'warning': ["Warn 1", "Warn 2"]
}]


var combined = Object.values(err.concat(warn).reduce(function(agg, item) {
  agg[item.id] = { ...agg[item.id], ...item}
  return agg;
}, {}));
console.log(combined)

about 4 years ago · Santiago Gelvez 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!