首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用另一个数组过滤对象数组-返回两个值

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

Stack Overflow用户
提问于 2021-02-17 17:19:53
回答 1查看 21关注 0票数 0

当用户在搜索框中输入时,我想通过名称和类型来过滤我的搜索,但是我只是得到了名称,而不是类型。

这是我的数组:

代码语言:javascript
复制
        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']
        }
    ]

我的反应是过滤我的数组并返回项目名称,同时输入

代码语言:javascript
复制
    const projectFilter = this.state.projects.filter( project => {
                return project.name.toLowerCase().includes(this.state.searchField.toLowerCase())
                || project.type.includes(this.state.searchField);
            })
EN

回答 1

Stack Overflow用户

发布于 2021-02-17 17:34:11

您的代码很好。我查过了。确保您的searchField如您所愿。

Check this out

代码语言:javascript
复制
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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66238991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档