Angular 2-如何过滤对象阵列?

时间:2017-06-15 17:01:31

标签: angular

我有如下json数组,我想检索一个与team1Id / team2Id匹配的所有对象。

游戏:[{             “id”:7935,             “location”:“Meadowbrook Ct.2”,             “locationId”:“loc-2”,             “team1”:“MADE Elite”,             “team1Id”:798,             “team1Score”:“40”,             “team2”:“Reisterstown Wolfpack”,             “team2Id”:797,             “team2Score”:“38”,             “时间”:“2016-07-09T08:00:00”         },

where

2 个答案:

答案 0 :(得分:1)

让我们假设你把json变成了对象形式
在打字稿中,它看起来像

list:Array<YourObject>=loadYourList();

filtered:Array<YourObject> = list.filter(x=>x.team1Id=='yourID'&& x.team2Id=='your_id');

答案 1 :(得分:0)

简单的方法是使用 lodash 库:

_.filter(games, function(game) {
    return game.team1Id === YOUR_ID && game.team2Id === YOUR_ID;
});

games:你的json bojects数组

YOUR_ID:您要与

进行比较的ID