Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!