Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda