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

102
Visualizações
Javascript counting with filter not working

I want to count the wins and losses out of an array of matches but for some reason it doesn't work. In theory it should work but both output 0.

Here is my code I'm using handlebars so the {{ work!

const matches = [{
  id: '9',
  primary_player: 'player1',
  secondary_player: 'player2',
  size: '5',
  result: '0'
}]

const losses = matches.filter(e => e.result == "0" && e.primary_player == "player1").length;

const wins = matches.filter(e => e.result == "1" && e.primary_player == "player1").length;

console.log(losses,wins)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It looks like the problem is just your double quotes wrapped around the primary_player check -- they're not needed in the evaluation. Also, I'm not sure that your length check at the end is useful in any way? Please do correct me, though, if you need it in there (and perhaps explain what you're trying to do with it).

Anyway, this should work (if your matches.result really is a boolean):

const losses = matches.filter(e => e.result === 0 && e.primary_player == user.nickname);

If matches.result is actually a string, which is the more probable data type, then you'll can drop the strict equality check (===):

const losses = matches.filter(e => e.result == 0 && e.primary_player == user.nickname);
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