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

243
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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