【发布时间】:2022-01-15 18:03:38
【问题描述】:
请检查此 filterFunction 并告诉我如何改进它。这些过滤器单独工作,但不能一起工作
const filterFunction = (a) => {
if (!includeKeyword && !searchVolume && !excludeKeyword && !longtailKeyword){
return true
} else if (includeKeyword && !searchVolume){
return a.text.toLowerCase().includes(includeKeyword.toLocaleLowerCase())
} else if (!includeKeyword && searchVolume){
return a.keyword_idea_metrics.avg_monthly_searches > parseInt(searchVolume)
} else if (!searchVolume && excludeKeyword){
return !a.text.includes(excludeKeyword)
} else if (!excludeKeyword && longtailKeyword){
return a.text.split(" ").length == longtailKeyword
} else {
return a.text.toLowerCase().includes(includeKeyword.toLocaleLowerCase()) && a.keyword_idea_metrics.avg_monthly_searches > parseInt(searchVolume) && !a.text.includes(excludeKeyword) && a.text.split(" ").length <= longtailKeyword
}
}
渲染函数
postRes.filter(filterFunction).map((data, index) => (
非常感谢您提前提供的帮助
【问题讨论】:
-
什么不起作用?
-
您需要在问题中提供更多信息:
includeKeyword、searchVolume、excludeKeyword和longtailKeyword是什么?您是否尝试基于这些布尔标志的 AND 或 OR 组合进行过滤?
标签: javascript reactjs filter react-hooks