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

246
Vistas
How to count values in a javascript object?

I want to get all the values that equal a certain number and count how many of each of the objects.

My code looks like this:

var countItems = {
    "aa":"70",
    "bb":"70",
    "cc":"80",
    "dd":"90",
    "ee":"90",
    "ff":"90"
}

Now what I want to do is count each on that is in the second half.

For example, there are two "70", one "80", and three 90. Then I can assign to variables:

var firstCounter  = ?? // 2
var secondCounter = ?? // 1
var thirdCounter  = ?? // 3

?? is I don't know what goes here.

If it was structed differently like the following, I could do it like this:

let firstCounter = 0;
for (let i = 0; i < countItems.length; i++) {
  if (countItems[i].status === '70') firstCounter++;
}

let secondCounter = 0;
for (let i = 0; i < countItems.length; i++) {
  if (countItems[i].status === '80') secondCounter++;
}

let thirdCounter = 0;
for (let i = 0; i < countItems.length; i++) {
  if (countItems[i].status === '90') thirdCounter++;
}

But the thing is, my original code which is what I have is not structured like that, so I'm not sure how to adapt it.

How can I count the items in the original list (var countItems) so that I can find out how much each value is?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could use Object.values(countItems) to get an array that looks like this: ["70","70","80","90","90","90"] then either use a for loop to conditionally increment whatever counters you want, or use something like Array.reduce or Array.filter to count the elements you need.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could use reduce to create a counted hash map like so:

const countItems = [
  { data: 'aa', status: '70' },
  { data: 'bb', status: '70' },
  { data: 'cc', status: '80' },
  { data: 'dd', status: '90' },
  { data: 'ee', status: '90' },
  { data: 'ff', status: '90' },
];

const countedHash = countItems.reduce((acc, curr) => {
  if (!acc[curr.status])
    acc[curr.status] = 1
  else
    acc[curr.status] += 1
  return acc
}, {})

/* print out the results */
console.log(countedHash)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can access object keys like this :

countItems["aa"] // it will return "70"

You can also loop on the object (if you want to do as you did in your example) :

for (const item in countItems) {
    console.log(countItems[item])
    if (countItems[item] == "70") firstCounter++;
    
}
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