Javascript筛选器方法不使用if和else if
我有一个包含多(10)个对象的对象 1对象如下所示:Javascript筛选器方法不使用if和else if,javascript,object,filter,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_fo
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
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);
});
我想过滤匹配上的对象(团队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);
}
});
我有两个变量来设置团队id:
var team1ID = 21206;
var team2ID = 21202;
我希望filter函数返回与teamID1和teamID2匹配的特定对象。如果gameInfo.team\u 1\u id==team1ID&&gameInfo.team\u 2\u id==team2ID
但是当我这样反转它时:else如果(gameInfo.team\u 1\u id==team2ID&&gameInfo.team\u 2\u id==team1ID)
那么对象game
总是空的……为什么
(inverted = 0)
这里是赋值,不是比较。结果值(0
)将始终为falsy,并且过滤器函数将返回一个空的game
数组
此外,您还应首先检查倒
标志,不要重复其他不必要的比较:
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;
});
如果要设置它,它应该如下所示:
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
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);
});
是的,目的是将一个值赋给“inversed”,当inversed=1时,我做某事,当inversed=0时,我做其他事情。稍后在我的代码中,我想知道TeamID何时被反转。因此,当gameInfo.team_1_id==team2ID&&gameInfo.team_2_id==team1id时,请参见。我仍然不知道为什么要使用
过滤器
然后返回特定对象(与过滤器匹配)并查看团队是否反转。是的,我的最后两个片段就是这样做的,不是吗?