用另一个数组过滤一个对象数组 - 返回两个值

时间:2021-02-17 09:19:53

标签: arrays reactjs filter

我想在用户在搜索框中键入时按名称和类型过滤我的搜索,但是我只是返回名称而不是按类型。

这是我的数组:

        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);
            })
    

1 个答案:

答案 0 :(得分:0)

你的代码没问题。我检查了。确保您的搜索字段如您所愿。

Check this out

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);
}
相关问题