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