search

Home  >  Q&A  >  body text

How to use search bar with two different values?

I tried adding another value (organization.name) to the search bar which was already running successfully, in order to not only filter the list of SearchTerms from organization.topic. But he didn't add it..

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

Here are the relevant parts of my return:

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

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

Hope you have some ideas

P粉238433862P粉238433862445 days ago457

reply all(2)I'll reply

  • P粉745412116

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

    Do not use || as you also want to check the organization name. Just concatenate the strings

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

    reply
    0
  • P粉083785014

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

    This is the solution:

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

    reply
    0
  • Cancelreply