我想在用户在搜索框中键入时按名称和类型过滤我的搜索,但是我只是返回名称而不是按类型。
这是我的数组:
const projects = [
{
id: 1,
name: 'protfolio',
type: ['react']
},
{
id: 2,
name: 'RoboFriends',
type: ['react']
},
{
id: 3,
name: 'Background-gradient',
type: ['html ', 'css ', 'javaScript']
},
{
id: 4,
name: 'Election-map',
type: ['html ', 'css ', 'javaScript']
},
{
id: 5,
name: 'To-Do-List',
type: ['react']
}
]
我对数组进行过滤并返回项目名称并键入的函数
const projectFilter = this.state.projects.filter( project => {
return project.name.toLowerCase().includes(this.state.searchField.toLowerCase())
|| project.type.includes(this.state.searchField);
})
答案 0 :(得分:0)
你的代码没问题。我检查了。确保您的搜索字段如您所愿。
function myFunction() {
const projects = [
{
id: 1,
name: 'protfolio',
type: ['react']
},
{
id: 2,
name: 'RoboFriends',
type: ['react']
},
{
id: 3,
name: 'Background-gradient',
type: ['html ', 'css ', 'javaScript']
},
{
id: 4,
name: 'Election-map',
type: ['html ', 'css ', 'javaScript']
},
{
id: 5,
name: 'To-Do-List',
type: ['react']
}
]
const projectFilter = projects.filter( project => {
return project.name.toLowerCase().includes('react')
|| project.type.includes('react');
})
document.getElementById("test").innerText = (projectFilter[2].name);
}