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

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

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..

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

thats the relevant part of my return:

  1. <TableBody>
  2. {filteredOrganizations.map((organization) => (
  3. <StyledTableRow key={organization.id}>
  4. ...
英文:

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..

  1. const filteredOrganizations = context.allOrganizations.filter((organization) =&gt; (organization.topic.searchTerms
  2. .map((term) =&gt; term.toLowerCase())
  3. .join(&quot; &quot;) ||
  4. organization.name)
  5. .includes(searchText.toLowerCase()))

thats the relevant part of my return:

  1. &lt;TableBody&gt;
  2. {filteredOrganizations.map((organization) =&gt; (
  3. &lt;StyledTableRow key={organization.id}&gt;
  4. ...

I hope you have an idea

答案1

得分: 0

不要使用 ||,因为您还想检查组织名称。
只需将字符串连接起来

  1. const filteredOrganizations = context.allOrganizations.filter(organization =>
  2. (
  3. organization.topic.searchTerms
  4. .map(term => term.toLowerCase()).join(' ')
  5. + ' ' + organization.name
  6. )
  7. .includes(searchText.toLowerCase()))
英文:

Don't use || since you want to check also the organization name.
Just concatenate the strings

  1. const filteredOrganizations = context.allOrganizations.filter(organization =&gt;
  2. (
  3. organization.topic.searchTerms
  4. .map(term =&gt; term.toLowerCase()).join(&#39; &#39;)
  5. + &#39; &#39; + organization.name
  6. )
  7. .includes(searchText.toLowerCase()))

答案2

得分: 0

以下是您要翻译的代码部分:

  1. const searchText = 'term5'; // 用户输入
  2. // 虚拟数据
  3. const context = {
  4. allOrganizations: [
  5. {
  6. name: 'Organization 1',
  7. topic: {
  8. searchTerms: ['term1', 'term2'],
  9. },
  10. },
  11. {
  12. name: 'Organization 2',
  13. topic: {
  14. searchTerms: ['term5', 'term6'],
  15. },
  16. },
  17. {
  18. name: 'Organization 3',
  19. topic: {
  20. searchTerms: ['term7', 'term8'],
  21. },
  22. },
  23. ],
  24. };
  25. // 过滤组织
  26. const filteredOrganizations = context.allOrganizations.filter((organization) =>
  27. (
  28. organization.topic.searchTerms
  29. // 去除空格并转换为小写
  30. .map((term) => term.trim().toLowerCase())
  31. // 将术语连接成单个字符串
  32. .join(' ') +
  33. // 添加组织名称
  34. ' ' +
  35. organization.name
  36. )
  37. .toLowerCase()
  38. // 检查搜索文本是否包含在字符串中
  39. .includes(searchText.trim().toLowerCase())
  40. );
  41. console.log(filteredOrganizations);

希望这可以帮助您。

英文:
  1. const searchText = &#39;term5&#39;; // user input
  2. // dummy data
  3. const context = {
  4. allOrganizations: [
  5. {
  6. name: &#39;Organization 1&#39;,
  7. topic: {
  8. searchTerms: [&#39;term1&#39;, &#39;term2&#39;],
  9. },
  10. },
  11. {
  12. name: &#39;Organization 2&#39;,
  13. topic: {
  14. searchTerms: [&#39;term5&#39;, &#39;term6&#39;],
  15. },
  16. },
  17. {
  18. name: &#39;Organization 3&#39;,
  19. topic: {
  20. searchTerms: [&#39;term7&#39;, &#39;term8&#39;],
  21. },
  22. },
  23. ],
  24. };
  25. // filter organizations
  26. const filteredOrganizations = context.allOrganizations.filter((organization) =&gt;
  27. (
  28. organization.topic.searchTerms
  29. // remove whitespace and convert to lowercase
  30. .map((term) =&gt; term.trim().toLowerCase())
  31. // join terms into a single string
  32. .join(&#39; &#39;) +
  33. // add organization name
  34. &#39; &#39; +
  35. organization.name
  36. )
  37. .toLowerCase()
  38. // check if search text is included in the string
  39. .includes(searchText.trim().toLowerCase())
  40. );
  41. console.log(filteredOrganizations);

答案3

得分: 0

这是解决方案:

  1. const filteredOrganizations = context.allOrganizations.filter(organization => {
  2. const searchTerms = organization.topic.searchTerms.map(term => term.toLowerCase()).join(" ");
  3. const organizationName = organization.name.toLowerCase();
  4. const searchString = `${searchTerms} ${organizationName}`;
  5. return searchString.includes(searchText.toLowerCase());
  6. });
英文:

This was the solution:

  1. const filteredOrganizations = context.allOrganizations.filter(organization =&gt; {
  2. const searchTerms = organization.topic.searchTerms.map(term =&gt; term.toLowerCase()).join(&quot; &quot;);
  3. const organizationName = organization.name.toLowerCase();
  4. const searchString = `${searchTerms} ${organizationName}`;
  5. return searchString.includes(searchText.toLowerCase());
  6. });

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:

确定