user1386906
user1386906

Reputation: 1179

Javascript filter method not working with if and else if

I have a object that contains multiple (10) objects.

1 object looks like this:

bracket: null
bracket_id: null
game_site: null
game_site_id: null
id: 88462
next_game_for_loser: null
next_game_for_loser_id: null
next_game_for_winner: null
next_game_for_winner_id: null
next_team_for_loser: 1
next_team_for_winner: 1
number_of_sets: 5
pool: Object
pool_id: 18739
pool_round: Object
pool_round_id: 21984
season: Object
season_id: 20167
start_time: "2013-03-04T13:00:00+01:00"
swiss_round: null
swiss_round_id: null
team_1: Object
team_1_id: 21202
team_1_score: 3
team_2: Object
team_2_id: 21206
team_2_score: 1
time_created: "2012-12-09T12:46:33.626319+00:00"
time_last_updated: "2012-12-09T12:46:33.626361+00:00"
timezone: "Europe/Amsterdam"
tournament: Object
tournament_id: 18519
winner: Object
winner_id: 21202

I want to filter the objects on matchups (team id's)

I filter like so:

var game = games.objects.filter(function (gameInfo) { 

            if (gameInfo.team_1_id == team1ID && gameInfo.team_2_id == team2ID) {
                return (gameInfo.team_1_id == team1ID && gameInfo.team_2_id == team2ID) && (inverted = 1);
            } 

            else if (gameInfo.team_1_id == team2ID && gameInfo.team_2_id  == team1ID) {
                return (gameInfo.team_1_id == team2ID && gameInfo.team_2_id  == team1ID) && (inverted = 0);
            }
        });

I got 2 variables to set the team id:

var team1ID = 21206;
var team2ID = 21202;

I want the filter function to return the specific object where it matches the teamID1 and teamID2. This works good if gameInfo.team_1_id == team1ID && gameInfo.team_2_id == team2ID But when i invert it like so: else if (gameInfo.team_1_id == team2ID && gameInfo.team_2_id == team1ID) Then the object game is always empty...Why is that?

Upvotes: 0

Views: 79

Answers (1)

Bergi
Bergi

Reputation: 664538

(inverted = 0)

Here you are assigning, not comparing. The resulting value (0) will always be falsy, and the filter function will return an empty game array.

Also, you should check for the inverted flag first, and not duplicate the other comparisons unnecessary:

var game = games.objects.filter(function (gameInfo) { 
    return inverted == 1
      ? gameInfo.team_1_id == team1ID && gameInfo.team_2_id == team2ID
      : gameInfo.team_1_id == team2ID && gameInfo.team_2_id == team1ID;
});

If you want to set it, it should look like this:

var inverted;
var game = games.objects.filter(function (gameInfo) { 
    if (gameInfo.team_1_id == team1ID && gameInfo.team_2_id == team2ID) {
        inverted = 0;
        return true;
    } else if (gameInfo.team_1_id == team2ID && gameInfo.team_2_id == team1ID) {
        inverted = 1;
        return true;
    } // else
        return false;
});

// Shorter, using the comma operator:

var game = games.objects.filter(function (gameInfo) { 
    return (gameInfo.team_1_id == team1ID && gameInfo.team_2_id == team2ID && (inverted = 1, true)
        || (gameInfo.team_1_id == team2ID && gameInfo.team_2_id == team1ID && (inverted = 0, true);
});

Upvotes: 2

Related Questions