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

299
Views
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 answers
Answer question

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 Report

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 Report

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 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!