【发布时间】:2018-03-15 20:04:20
【问题描述】:
我们制作了一个视频游戏数据库,我们需要按游戏和排名过滤人们。该对象的结构如下:
[{
"key": 86,
"position": [
40.369,
-3.5419
],
"info": {
"gamesplaying": [
{
"gameid": 2,
"rankid": 8
},
{
"gameid": 4,
"rankid": 8
},
{
"gameid": 5,
"rankid": 14
}
],
"city": "Kersey",
"age": 42,
"name": "Fisher Hayden",
"id": 2
}
},
{
"key": 87,
"position": [
40.554,
-3.7578
],
"info": {
"gamesplaying": [
{
"gameid": 2,
"rankid": 2
}
],
"city": "Ahwahnee",
"age": 24,
"name": "Aurora Rivers",
"id": 35
}
}]
我们需要按游戏 id 和排名 id 执行过滤,当使用对象而不是函数作为过滤器时,_.filter 似乎对我们有帮助。这不介意游戏数组中的元素数量或顺序,所以它完美!性能比几个嵌套的 for 循环要好得多,所以我们希望保留这个函数。
我们的问题是我们现在添加了一个年龄过滤器。所以我们不需要一个确切的年龄,而是两个值之间的年龄。
函数现在看起来像这样:
let markers = this.props.markers;
if(this.state.rankidFilter > 0 || this.state.gameidFilter > 0 || this.state.minAgeFilter > 0 || this.state.maxAgeFilter > 0) {
let gameFilters = {};
if (this.state.gameidFilter > 0) gameFilters = {...gameFilters, gameid: parseInt(this.state.gameidFilter, 10)};
if (this.state.rankidFilter > 0) gameFilters = {...gameFilters, rankid: parseInt(this.state.rankidFilter, 10)};
let newMarkers =_.filter(this.props.markers, {info: {gamesplaying: [gameFilters]}});
markers = newMarkers;
}
我们尝试了一些东西来为年龄属性添加过滤对象函数或条件,因此值介于 2 个数字之间,但我们没有找到正确的方法。
请帮忙,我们想保留 _.filter 功能。提前致谢
编辑:像 {info: {gamesplaying: [{gameid: parseInt(this.state.gameidFilter, 10), rankid: parseInt(this.state.rankidFilter, 10)}]} 的对象,age: _.between (20, 55)} 在该值中考虑一系列年龄
【问题讨论】:
标签: javascript json reactjs lodash