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

176
Vistas
How do I go about creating a javascript object that stores the count of unique objects?

Lets say I have an array

['Student', 'Progress', 'Student', 'Area']

I would like to have an object that looks like this...

 {
    {
      value: "Student",
      count: 2
    },
    {
      value: "Progress",
      count: 1
    }
 }

The code I am using currently is this... which clearly is wrong. Any help would be appreciated.

    const counts = [];
    for (let i = 0; i < array.length; i++) {
        var object = {
                value: counts[array[i]],
                count: (1 + (counts[array[i]] || 0))
        }
        counts.push(object);
    };
    return counts;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could take an object for counting and map the entries of the object as new object.

const
    data = ['Student', 'Progress', 'Student', 'Area'],
    result = Object
        .entries(data.reduce((r, s) => {
            r[s] = (r[s] || 0) + 1;
            return r;
        }, {}))
        .map(([value, count]) => ({ value, count }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza 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