Javascript 过滤方法不适用于 if 和 else if

标签 javascript object filter

我有一个包含多个 (10) 个对象的对象。

1 个对象如下所示:

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

我想过滤比赛中的对象(团队 ID)

我像这样过滤:

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);
            }
        });

我有 2 个变量来设置团队 ID:

var team1ID = 21206;
var team2ID = 21202;

我希望过滤器函数返回与 teamID1 和 teamID2 匹配的特定对象。如果 gameInfo.team_1_id == team1ID && gameInfo.team_2_id == team2ID 则效果很好 但是当我像这样反转它时: else if (gameInfo.team_1_id == team2ID && gameInfo.team_2_id == team1ID) 那么对象 game 总是空的......为什么是吗?

最佳答案

(inverted = 0)

这里你是分配,而不是比较。结果值 (0) 始终为假值,并且过滤函数将返回空的 game 数组。

此外,您应该首先检查 inverted 标志,并且不要重复其他不必要的比较:

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;
});

如果你想设置它,它应该看起来像这样:

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);
});

关于Javascript 过滤方法不适用于 if 和 else if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14568152/

相关文章:

javascript - 如何使用nodejs监听多个ips?

c++ - 将参数限制为 C++ 中的特定类和派生类

javascript - 如何在 JS 对象中获取父属性?

amazon-web-services - 如何在 AWS Elasticache (Redis) 中使用布隆过滤器?

ios - 如何告诉 ABPeoplePickerNavigationController 只列出有电子邮件地址的联系人?

javascript - 如何永远运行 Meteor?使用 3rd 方数据库好吗?

javascript - 使用 jquery 对复选框列表进行验证

javascript - 在 localStorage 上捕获 QUOTA_EXCEEDED_ERR

javascript - 为什么 datepicker() 接受参数 'getDate' ?

python - 根据条件将 Pandas DataFrame 中的一行替换为 'NaN'