Arrays 多级JSON过滤器
我有一个json: 我希望根据运行时进行筛选。开始和结束 我已经试过了:Arrays 多级JSON过滤器,arrays,json,reactjs,filter,momentjs,Arrays,Json,Reactjs,Filter,Momentjs,我有一个json: 我希望根据运行时进行筛选。开始和结束 我已经试过了: const filtered = array.filter((project) => { if (!project.hidden) { if (project.runTimes.filter((runTime) => { if (moment(runTime.start).isSameOrAfter(context.searchFr
const filtered = array.filter((project) => {
if (!project.hidden) {
if (project.runTimes.filter((runTime) => {
if (moment(runTime.start).isSameOrAfter(context.searchFrom) &&
moment(runTime.end).isSameOrBefore(context.searchTo)) {
return runTime;
}
}).length > 0) {
return project;
}
}
});
遗憾的是,当第二个运行时存在于项目对象运行时数组中时,它就不起作用了
有人有什么想法吗?过滤功能有点差。我认为如果围绕过滤器的语句令人困惑,我会选择更简单的。这对我有用:
const result = array.projects.filter((project) => {
return !project.hidden && project.runTimes.filter(runTime => {
return moment(runTime.start).isSameOrAfter("2020-W01") &&
moment(runTime.end).isSameOrBefore("2020-W12")
})
});
array.filter
中array
的值是多少?这与this.state.projects.filter
相同吗?是的,实际上数组=this.state.projects。而且,每当在react中更新状态时,这不会从数组中清除任何筛选项。因此,虽然它返回false,但它不会更新数组,该数组在最后一次返回后仍保留filterSo之前的相同项。因此,它需要以下返回时刻(runTime.start)。isSameOrAfter(“2020-W01”)&&矩(runTime.end)。isSameOrBefore(“2020-W12”)})。长度>0;但即使如此,它也只有在超出运行时数组中第一个的范围时才会隐藏。您是否尝试使用Array.prototype.some()
而不是使用长度检查进行筛选?
const result = array.projects.filter((project) => {
return !project.hidden && project.runTimes.filter(runTime => {
return moment(runTime.start).isSameOrAfter("2020-W01") &&
moment(runTime.end).isSameOrBefore("2020-W12")
})
});