Heim  >  Fragen und Antworten  >  Hauptteil

Wie verwende ich die Suchleiste mit zwei verschiedenen Werten?

Ich habe versucht, der Suchleiste einen weiteren Wert (Organisationsname) hinzuzufügen, der bereits erfolgreich funktionierte, um nicht nur die SearchTerms-Liste aus Organisation.Thema zu filtern. Aber er hat es nicht hinzugefügt..

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

Das ist der relevante Teil meiner Rücksendung:

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

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

Ich hoffe, Sie haben ein paar Ideen

P粉238433862P粉238433862380 Tage vor403

Antworte allen(2)Ich werde antworten

  • 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()))

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

    Antwort
    0
  • StornierenAntwort