英文:
How can I use my searchbar with two different values?
问题
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'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
答案1
得分: 0
不要使用 ||
,因为您还想检查组织名称。
只需将字符串连接起来
const filteredOrganizations = context.allOrganizations.filter(organization =>
(
organization.topic.searchTerms
.map(term => term.toLowerCase()).join(' ')
+ ' ' + organization.name
)
.includes(searchText.toLowerCase()))
英文:
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()))
答案2
得分: 0
以下是您要翻译的代码部分:
const searchText = 'term5'; // 用户输入
// 虚拟数据
const context = {
allOrganizations: [
{
name: 'Organization 1',
topic: {
searchTerms: ['term1', 'term2'],
},
},
{
name: 'Organization 2',
topic: {
searchTerms: ['term5', 'term6'],
},
},
{
name: 'Organization 3',
topic: {
searchTerms: ['term7', 'term8'],
},
},
],
};
// 过滤组织
const filteredOrganizations = context.allOrganizations.filter((organization) =>
(
organization.topic.searchTerms
// 去除空格并转换为小写
.map((term) => term.trim().toLowerCase())
// 将术语连接成单个字符串
.join(' ') +
// 添加组织名称
' ' +
organization.name
)
.toLowerCase()
// 检查搜索文本是否包含在字符串中
.includes(searchText.trim().toLowerCase())
);
console.log(filteredOrganizations);
希望这可以帮助您。
英文:
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);
答案3
得分: 0
这是解决方案:
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());
});
英文:
This was 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());
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论