如何过滤属于数组值的对象?
鉴于以下数据如何过滤属于数组值的对象?
[
{
"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
只显示对象我知道我只能在数组上运行过滤器方法,但我无法弄清楚如何循环数组内部的对象并再次返回整个对象。任何帮助将不胜感激。
尝试:
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"
}
]
}
]
我认为这是你想要的。
您好CRice,感谢您花时间回答我,但这似乎并没有工作......我试图找出基本上如何取回日期和所有游戏相同的阵列,但这些比赛应该只显示他们是否=曲棍球。 – Blake
我已经更新了我的答案,并在过滤器完成后提供了最终对象。仔细查看一下,如果这就是你想要的,很好,但是否则让我知道你期望找回哪些数据,并且我可以修改它以产生它。 – CRice
您好CRice,我使用扩展运算符时一直ketting错误,所以我用date:item.date,似乎工作。我遇到的唯一问题是,这也是没有任何曲棍球比赛(比赛阵列是空的)的返回对象,但由于当天除了曲棍球之外还有其他比赛,所以我回来的日期只与游戏数组为空......这是导致我渲染空的组件......只是想知道是否有解决方法?我的实际数据比我上面给出的样本大很多,所以有些日期没有曲棍球比赛。 – Blake
我认为下面的代码应该做的伎俩:
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"))
这可能有助于太https://stackoverflow.com/questions/38375646/filtering-array-of-objects-with-arrays-based-嵌套值 –