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

184
Views
Uso de Object.entries()

¿Hay alguna forma de simplificar este código con el uso de Object.entries()? Quiero eliminar el nuevo Mapa ().

 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)

Intentó:

 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)

La salida debe seguir siendo la misma.

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

0

Puede usar Array.prototype.map() para crear los pares clave/valor para el new Map() .

 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() no es útil porque las claves son los índices de matriz, no las propiedades de id .

about 4 years ago · Santiago Gelvez Report

0

Si espera más de 1 elemento por matriz, puede hacer:

 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)

Si el objeto final depende de esas otras matrices y sabe de antemano que tienen exactamente una longitud de 1, entonces es más simple:

 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

Las entradas de objetos son para objetos, no para una matriz. Sin embargo, puede agrupar usando reduce en objeto, luego hacer que sus valores se conviertan en una matriz.

 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!