Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

307
Vistas
How to count the no of occurences of a certain key in an array of object & append the count to each key's value

How to count the no of occurences of the 'value' key in a object inside an array & append the count to each value if any.

Here 'a' is the source data

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"}
]

What i want to achieve is as following

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 Respuestas
Responde la pregunta

0

You can use Array.map and save the value

I think this is more readable than using 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 Denunciar

0

You can make use of Map as a place where you can store count of number of occurence.

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda