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

301
Visualizações
Cómo contar el número de ocurrencias de una determinada clave en una matriz de objetos y agregar el recuento al valor de cada clave

Cómo contar el número de ocurrencias de la clave ' valor ' en un objeto dentro de una matriz y agregar el conteo a cada valor, si corresponde.

Aquí 'a' son los datos de origen

 var a = [ { id: 1, value: "10000"}, { id: 2, value: "20000"}, { id: 3, value: "30000"}, { id: 4, value: "10000"}, { id: 5, value: "20000"}, { id: 6, value: "40000"}, { id: 7, value: "10000"}, { id: 8, value: "70000"} ]

Lo que quiero lograr es lo siguiente

 result = [ { id: 1, value: "10000"}, { id: 2, value: "20000"}, { id: 3, value: "30000"}, { id: 4, value: "10000 (1)"}, { id: 5, value: "20000 (1)"}, { id: 6, value: "40000"}, { id: 7, value: "10000 (2)"}, { id: 8, value: "10000 (3)"} ]
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Puede usar Array.map y guardar el valor

Creo que esto es más legible que usar Map

 const values = {}, b = a.map(({ id, value }) => { values[value] = (values[value] || 0) + 1; // assign and/or increment if (values[value] > 1) value += ` (${values[value]-1})`; return { id, value }; }); console.log(b)
 <script> var a = [ { id: 1, value: "10000"}, { id: 2, value: "20000"}, { id: 3, value: "30000"}, { id: 4, value: "10000"}, { id: 5, value: "20000"}, { id: 6, value: "40000"}, { id: 7, value: "10000"}, { id: 8, value: "10000"} ]</script>

about 4 years ago · Santiago Trujillo Relatório

0

Puede hacer uso de Map como un lugar donde puede almacenar el recuento del número de ocurrencias.

 const a = [ { id: 1, value: '10000' }, { id: 2, value: '20000' }, { id: 3, value: '30000' }, { id: 4, value: '10000' }, { id: 5, value: '20000' }, { id: 6, value: '40000' }, { id: 7, value: '10000' }, { id: 8, value: '70000' }, ]; const map = new Map(); const result = a.map((o) => { const valueInMap = map.get(o.value); map.set(o.value, (valueInMap ?? 0) + 1); return valueInMap ? { ...o, value: `${o.value} (${valueInMap})` } : o; }); console.log(result);

about 4 years ago · Santiago Trujillo Relatório

0

 var a = [ { id: 1, value: '10000' }, { id: 2, value: '20000' }, { id: 3, value: '30000' }, { id: 4, value: '10000' }, { id: 5, value: '20000' }, { id: 6, value: '40000' }, { id: 7, value: '10000' }, { id: 8, value: '10000' }, ]; const hashMap = {}; const newArr = a.map((el) => { const temp = { ...el }; if (!hashMap[el.value]) { hashMap[el.value] = 1; } else if (hashMap[el.value]) { temp.value = `${el.value} (${hashMap[el.value]})`; hashMap[el.value] += 1; } return temp; }); console.log(newArr);

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