skip to Main Content

I’ve tried to add another value (organization.name) to my already successfully working searchbar, so that not only the list of SearchTerms from my organization.topic were filtered. But he doesnt add it..

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

thats the relevant part of my return:

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

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

I hope you have an idea

2

Answers


  1. Don’t use || since you want to check also 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()))
    
    Login or Signup to reply.
  2. const searchText = 'term5'; // user input
    
    // dummy data
    const context = {
      allOrganizations: [
        {
          name: 'Organization 1',
          topic: {
            searchTerms: ['term1', 'term2'],
          },
        },
        {
          name: 'Organization 2',
          topic: {
            searchTerms: ['term5', 'term6'],
          },
        },
        {
          name: 'Organization 3',
          topic: {
            searchTerms: ['term7', 'term8'],
          },
        },
      ],
    };
    
    // filter organizations
    const filteredOrganizations = context.allOrganizations.filter((organization) =>
      (
        organization.topic.searchTerms
          // remove whitespace and convert to lowercase
          .map((term) => term.trim().toLowerCase())
          // join terms into a single string
          .join(' ') +
        // add organization name
        ' ' +
        organization.name
      )
        .toLowerCase()
    
        // check if search text is included in the string
        .includes(searchText.trim().toLowerCase())
    );
    
    console.log(filteredOrganizations);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search