【发布时间】:2018-11-14 17:36:53
【问题描述】:
我有一个实时搜索下拉组件,它通过搜索词过滤一组对象。它按标题过滤我的对象,然后返回所有相关对象的列表。这工作正常。
当前:
数据结构
data: [
{ id: 1, title: 'Some title here' },
{ id: 2, title: 'Another title' },
{ id: 3, title: 'last title' },
]
组件
<LiveSearch
term={term}
data={data} />
实时搜索组件内部
按术语过滤数据并呈现列表
return data
.filter(item => item.title.toLowerCase().includes(term.toLowerCase())
.map((item, idx) => <li key={idx}>{item.title}</li>
我的搜索对象变得越来越高级,我希望能够将一组属性名称传递给我的组件,我想将其与搜索词进行比较。
我的思考过程是循环遍历对象属性,如果其中一个属性与术语匹配,则循环中断并返回 true,将该对象添加到要显示的项目列表中。
目标
数据结构
data: [
{ id: 1, country: 'Canada', title: 'Some title here' },
{ id: 2, country: 'Australia', title: 'Another title' },
{ id: 3, country: 'Netherlands', title: 'last title' },
]
组件
<LiveSearch
searchFields={['country', 'title']}
term={term}
data={data} />
内部组件过滤
return data
.filter(item => {
// Dynamic filtering of terms here
})
.map((item, idx) => <li key={idx}>{item.title}</li>
在过滤器内部,我试图通过数组循环并动态生成与此类似的逻辑
item.searchFields[0].toLowerCase().includes(term.toLowerCase()) ||
item.searchFields[1].toLowerCase().includes(term.toLowerCase())
但显然可以循环无限数量的搜索字段/属性
【问题讨论】:
标签: javascript arrays filter