Angular 角度2-如何过滤对象数组?
我有如下的json数组,我想检索一个与team1Id/team2Id匹配的所有对象 游戏:[{ “id”:7935, “位置”:“梅多布鲁克Ct.2”, “locationId”:“loc-2”, “团队1”:“打造精英”, “team1Id”:798, “team1Score”:“40”, “团队2”:“Reisterstown Wolfpack”, “team2Id”:797, “team2Score”:“38”, “时间”:“2016-07-09T08:00:00” },Angular 角度2-如何过滤对象数组?,angular,Angular,我有如下的json数组,我想检索一个与team1Id/team2Id匹配的所有对象 游戏:[{ “id”:7935, “位置”:“梅多布鲁克Ct.2”, “locationId”:“loc-2”, “团队1”:“打造精英”, “team1Id”:798, “team1Score”:“40”, “团队2”:“Reisterstown Wolfpack”, “team2Id”:797, “team2Score”:“38”, “时间”:“2016-07-09T08:00:00” }, {
{
"id": 7936,
"location": "Meadowbrook Ct. 4",
"locationId": "loc-4",
"team1": "DC Premier",
"team1Id": 801,
"team1Score": "39",
"team2": "Severn Elite",
"team2Id": 804,
"team2Score": "36",
"time": "2016-07-09T08:00:00"
},
many more
]
简单的方法是使用lodash库:
_.filter(games, function(game) {
return game.team1Id === YOUR_ID && game.team2Id === YOUR_ID;
});
游戏
:您的json对象数组
您的\u ID
:您想要与之比较的ID假设您将json转换为对象形式在typescript中,它看起来像
list:Array=loadYourList();
filtered:Array=list.filter(x=>x.team1Id=='yourID'&&x.team2Id=='your_id');
Doest ES6有一个用于筛选.array.filter(x=>x.team1Id==YOURID)的助手函数;语法更清晰。
filtered:Array<YourObject> = list.filter(x=>x.team1Id=='yourID'&& x.team2Id=='your_id');