简体   繁体   English

如何使用“过滤器”和“映射”判断列表是否为空

[英]How to tell if the list is empty using "filter" and "map"

      list
        .filter((val) => {
          if (
            val.name.toLocaleLowerCase().includes(input))
          ) {
            return val
          }
        })
        .map((val, idx) => (
          <Li
            key={idx}
          />
        ))

Using this structure, I wanted to do something if returns empty.使用这个结构,我想在返回空时做一些事情。

Ex: When searching for "abc" and there is no "abc" in the list, a message appears saying that "abc" was not found.例如:当搜索“abc”并且列表中没有“abc”时,会出现一条消息说找不到“abc”。

Several things to change:要改变的几件事:

1. Your filter expression is only working by accident 1.您的过滤器表达式只是偶然起作用

list
    .filter((val) => {
      if (
        val.name.toLocaleLowerCase().includes(input))
      ) {
        return val
      }
    })

can be just可以只是

list
    .filter(val => val.name.toLocaleLowerCase().includes(input)

Remember that the function inside filter just returns a true or false .请记住, filter内的函数只返回truefalse By chance your expression, which returns the actual list element, is interpreted as true , and when your expression does not match, you don't return anything, which has the same effect as returning false .偶然地,您返回实际列表元素的表达式被解释为true ,并且当您的表达式不匹配时,您不返回任何内容,这与返回false具有相同的效果。 Expressing it as above makes it clearer what you are doing (and is very marginally faster).如上表达它可以更清楚地了解你在做什么(并且速度非常快)。

2. To get your error message, save the output of the filter so you can test its length. 2. 要获取错误消息,请保存过滤器的输出,以便测试其长度。

const matches = list.filter(val => val.name.toLocaleLowerCase().includes(input)

const output =  (matches.length>0) ? 
    matches.map((val, idx) => (
      <Li
        key={idx}
      />
    )) : (
      <div>Not found</div>
    )

You can tell if the list is empty using "filter" and "map" by using following JavaScript Code:您可以使用以下 JavaScript 代码使用“过滤器”和“映射”判断列表是否为空:

list
.filter((value) => (value!==undefined) && (value!==null) && value.name.toLocaleLowerCase().includes(input)))
.map((value, id) => (
          <Li
            key={id}
          />
))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 React CLI 中使用 map 和 filter 列出另一个列表中的数据(评论列表和回复列表) - How to list a data inside another list using map and filter in React CLI (Comments List and Reply List) 如何让猫鼬列出集合中的所有文档? 告诉集合是否为空? - How to get Mongoose to list all documents in the collection? To tell if the collection is empty? 使用ng-repeat和filter,如何判断哪些项目可见? - Using ng-repeat and filter, how to tell which items are visible? 如何在 typescript 中使用 map-reduce 过滤具有属性最大值的列表? - How to filter a list with max value of an attribute using map-reduce in typescript? 如何使用 Lodash `_.filter()` 过滤以下列表 - How to filter the following list using Lodash `_.filter()` 如何在 JS 中使用 map 制作过滤器 - How to make filter using map in JS 如何使用 map 和过滤器修改原始阵列 - How to modify origninal array using map and filter javascript:按空格分割字符串并使用地图和过滤器制作列表 - javascript: split string by space and make list using map and filter 使用 map 呈现列表,但根据用户在前端的查询进行过滤 - render a list using map but filter base on user's query in frontend 想知道如何使用另一个列表过滤列表 - Wondering how to filter a list using another list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM