如何使用具有兩個不同值的搜尋欄?
P粉238433862
P粉238433862 2023-09-07 17:28:41
0
2
401

我嘗試在已經成功運行的搜尋欄中新增另一個值 (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粉238433862
P粉238433862

全部回覆(2)
P粉745412116

不要使用||,因為您還想檢查組織名稱。 只需連接字串即可

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

這就是解決方案:

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());
    });
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!