搜索

首页  >  问答  >  正文

如何使用具有两个不同值的搜索栏?

我尝试向已经成功运行的搜索栏添加另一个值 (organization.name),以便不仅过滤来自organization.topic 的SearchTerms 列表。但他没有添加它..

const filteredOrganizations = context.allOrganizations.filter((organization) => (organization.topic.searchTerms
        .map((term) => term.toLowerCase())
        .join(" ") ||
        organization.name)
        .includes(searchText.toLowerCase()))

这是我的退货的相关部分:

<TableBody>
                            {filteredOrganizations.map((organization) => (

                                <StyledTableRow key={organization.id}>
...

希望你有想法

P粉238433862P粉238433862449 天前464

全部回复(2)我来回复

  • P粉745412116

    P粉7454121162023-09-08 11:29:47

    不要使用||,因为您还想检查组织名称。 只需连接字符串即可

    const filteredOrganizations = context.allOrganizations.filter(organization => 
    (
      organization.topic.searchTerms
        .map(term => term.toLowerCase()).join(' ')
        + ' ' + organization.name
    )
    .includes(searchText.toLowerCase()))

    回复
    0
  • P粉083785014

    P粉0837850142023-09-08 09:42:26

    这就是解决方案:

    const filteredOrganizations = context.allOrganizations.filter(organization => {
            const searchTerms = organization.topic.searchTerms.map(term => term.toLowerCase()).join(" ");
            const organizationName = organization.name.toLowerCase();
            const searchString = `${searchTerms} ${organizationName}`;
    
            return searchString.includes(searchText.toLowerCase());
        });
    

    回复
    0
  • 取消回复