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

69
Views
Efficient solutuon to Filter objects inside objects inside an array

I have an array of objects containing more objects.

const latestMatchesArray = [
    {
      team1: {
        team_id: '1234',
        teamName: 'abc 101',
        score: 1,
      },
      team2: {
        team_id: '4321',
        teamName: 'cba 101',
        score: 0,
      },
    },
    {
      team1: {
        team_id: '1234',
        teamName: 'abc 101',
        score: 0,
      },
      team2: {
        team_id: '4321',
        teamName: 'cba 101',
        score: 1,
      },
    },
  ]

Each object contains a match between two teams, after the match, the scores are updated. Winner gets 1, loser gets 0.

Now I want to filter the array and get only teams with score = 1.

How to do this efficiently?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Not sure if this is what you're looking for, but to get an array of teams that won, you can use Array.prototype.reduce() like this:

const winners = latestMatchesArray.reduce(
    (teams, match) => [
        ...teams,
        ...[match.team1, match.team2].filter(
            team => team.score === 1
        )
    ],
    []
);
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!