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

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

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