如何使用我的搜索栏具有两个不同的值?

huangapple go评论87阅读模式
英文:

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) =&gt; (organization.topic.searchTerms
        .map((term) =&gt; term.toLowerCase())
        .join(&quot; &quot;) ||
        organization.name)
        .includes(searchText.toLowerCase()))

thats the relevant part of my return:

&lt;TableBody&gt;
                            {filteredOrganizations.map((organization) =&gt; (

                                &lt;StyledTableRow key={organization.id}&gt;
...

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 =&gt; 
(
  organization.topic.searchTerms
    .map(term =&gt; term.toLowerCase()).join(&#39; &#39;)
    + &#39; &#39; + 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 = &#39;term5&#39;; // user input

// dummy data
const context = {
  allOrganizations: [
    {
      name: &#39;Organization 1&#39;,
      topic: {
        searchTerms: [&#39;term1&#39;, &#39;term2&#39;],
      },
    },
    {
      name: &#39;Organization 2&#39;,
      topic: {
        searchTerms: [&#39;term5&#39;, &#39;term6&#39;],
      },
    },
    {
      name: &#39;Organization 3&#39;,
      topic: {
        searchTerms: [&#39;term7&#39;, &#39;term8&#39;],
      },
    },
  ],
};

// filter organizations
const filteredOrganizations = context.allOrganizations.filter((organization) =&gt;
  (
    organization.topic.searchTerms
      // remove whitespace and convert to lowercase
      .map((term) =&gt; term.trim().toLowerCase())
      // join terms into a single string
      .join(&#39; &#39;) +
    // add organization name
    &#39; &#39; +
    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 =&gt; {
        const searchTerms = organization.topic.searchTerms.map(term =&gt; term.toLowerCase()).join(&quot; &quot;);
        const organizationName = organization.name.toLowerCase();
        const searchString = `${searchTerms} ${organizationName}`;

        return searchString.includes(searchText.toLowerCase());
    });

huangapple
  • 本文由 发表于 2023年6月13日 18:07:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463791.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定