2017-10-20 103 views
0

鑑於以下數據如何過濾屬於數組值的對象?

[ 
    { 
    "date": "2017-10-04", 
    "games": [ 
     { 
     "notes": "Game was played", 
     "time": "2017-10-04T20:24:30+00:00", 
     "sport": "hockey", 
     "owner": "steve", 
     "players": "10", 
     "game_id": 1 
     }, 
     { 
     "notes": "Game was played", 
     "time": "2017-10-04T12:35:30+00:00", 
     "sport": "lacrosse", 
     "owner": "steve", 
     "players": "6", 
     "game_id": 2 
     }, 
     { 
     "notes": "Game was played", 
     "time": "2017-10-04T10:12:30+00:00", 
     "sport": "hockey", 
     "owner": "henry", 
     "players": "10", 
     "game_id": 4 
     } 
    ] 
    }, 
    { 
    "date": "2017-10-14", 
    "games": [ 
     { 
     "notes": "Game was played", 
     "time": "2017-10-14T20:32:30+00:00", 
     "sport": "hockey", 
     "owner": "steve", 
     "players": "4", 
     "game_id": 3 
     }, 
     { 
     "notes": "Game was played", 
     "time": "2017-10-14T20:34:30+00:00", 
     "sport": "soccer", 
     "owner": "john", 
     "players": "12", 
     "game_id": 5 
     } 
    ] 
    } 
] 

我怎麼過濾掉的對象,這樣我只顯示曲棍球比賽於該日播放。本質上我需要相同的對象數組,但只有當遊戲鍵= sport: hockey

只顯示對象我知道我只能在數組上運行過濾器方法,但我無法弄清楚如何循環數組內部的對象並再次返回整個對象。任何幫助將不勝感激。

+0

這可能有助於太https://stackoverflow.com/questions/38375646/filtering-array-of-objects-with-arrays-based-嵌套值 –

回答

1

嘗試:

const filtered = yourArray.map(item => ({...item, games: item.games.filter(game => game.sport === 'hockey')}) 

// When run, this produces: 
[ 
    { 
     "date": "2017-10-04", 
     "games": [ 
      { 
       "game_id": 1, 
       "notes": "Game was played", 
       "owner": "steve", 
       "players": "10", 
       "sport": "hockey", 
       "time": "2017-10-04T20:24:30+00:00" 
      }, 
      { 
       "game_id": 4, 
       "notes": "Game was played", 
       "owner": "henry", 
       "players": "10", 
       "sport": "hockey", 
       "time": "2017-10-04T10:12:30+00:00" 
      } 
     ] 
    }, 
    { 
     "date": "2017-10-14", 
     "games": [ 
      { 
       "game_id": 3, 
       "notes": "Game was played", 
       "owner": "steve", 
       "players": "4", 
       "sport": "hockey", 
       "time": "2017-10-14T20:32:30+00:00" 
      } 
     ] 
    } 
] 

我認爲這是你想要的。

+0

您好CRice,感謝您花時間回答我,但這似乎並沒有工作......我試圖找出基本上如何取回日期和所有遊戲相同的陣列,但這些比賽應該只顯示他們是否=曲棍球。 – Blake

+0

我已經更新了我的答案,並在過濾器完成後提供了最終對象。仔細查看一下,如果這就是你想要的,很好,但是否則讓我知道你期望找回哪些數據,並且我可以修改它以產生它。 – CRice

+0

您好CRice,我使用擴展運算符時一直ketting錯誤,所以我用date:item.date,似乎工作。我遇到的唯一問題是,這也是沒有任何曲棍球比賽(比賽陣列是空的)的返回對象,但由於當天除了曲棍球之外還有其他比賽,所以我回來的日期只與遊戲數組爲空......這是導致我渲染空的組件......只是想知道是否有解決方法?我的實際數據比我上面給出的樣本大很多,所以有些日期沒有曲棍球比賽。 – Blake

0

我認爲下面的代碼應該做的伎倆:

var x=[ 
{ 
"date": "2017-10-04", 
"games": [ 
    { 
    "notes": "Game was played", 
    "time": "2017-10-04T20:24:30+00:00", 
    "sport": "hockey", 
    "owner": "steve", 
    "players": "10", 
    "game_id": 1 
    }, 
    { 
    "notes": "Game was played", 
    "time": "2017-10-04T12:35:30+00:00", 
    "sport": "lacrosse", 
    "owner": "steve", 
    "players": "6", 
    "game_id": 2 
    }, 
    { 
    "notes": "Game was played", 
    "time": "2017-10-04T10:12:30+00:00", 
    "sport": "hockey", 
    "owner": "henry", 
    "players": "10", 
    "game_id": 4 
    } 
] 
}, 
{ 
"date": "2017-10-14", 
"games": [ 
    { 
    "notes": "Game was played", 
    "time": "2017-10-14T20:32:30+00:00", 
    "sport": "hockey", 
    "owner": "steve", 
    "players": "4", 
    "game_id": 3 
    }, 
    { 
    "notes": "Game was played", 
    "time": "2017-10-14T20:34:30+00:00", 
    "sport": "soccer", 
    "owner": "john", 
    "players": "12", 
    "game_id": 5 
    } 
] 
}] 


function filterByDate (data, date){ 

    return data.filter(function(entry){ 
    return sameDay(new Date(entry.date), date) 

    }) 
} 

function filterBySport(data, sport){ 
    data.forEach(function(entry){ 
    entry.games=entry.games.filter(function(entry2){ 
     return entry2.sport===sport 
    }) 
    }) 
    return data 
} 

function sameDay(date1, date2){ //helper function to check for date equality 
    return date1.getFullYear()===date2.getFullYear() && 
      date1.getMonth()===date2.getMonth() && 
     date1.getDay()===date2.getDay() 
} 

function myFilter(data, date, sportType){ // the final function you have to call 
    return filterBySport(filterByDate(data, date), sportType) 
} 

console.log(myFilter(x, new Date("2017-10-04"), "hockey")) 
相關問題