简体   繁体   English

为什么console.log没有显示任何内容?

[英]Why console.log shows me nothing?

I have a map function, which brings me a lot of strings with different values in some cases (before the map function, I also have a filter function too). 我有一个map函数,在某些情况下,它带给我很多带有不同值的字符串(在map函数之前,我也有一个filter函数)。 1st: I'm sure the returns are strings, since I console.log(typeof thing.name) and it returns me, well, strings. 1st:我确定返回的是字符串,因为我console.log(typeof thing.name)并且它返回了我字符串。

In some cases it brings me a lot of strings and in another cases it me brings me nothing, since the return depends on the filter. 在某些情况下,它带给我很多字符串,而在另一些情况下,它带给我什么,因为返回取决于过滤器。

The problem is: when the filter brings me no results, the console log do not print anything. 问题是:当过滤器未显示任何结果时,控制台日志不会打印任何内容。 I know, it makes sense. 我知道,这很有意义。 But I'm trying to make a conditional which returns me something like "undefined, null, false/true" when filter returns me 0/none/nothing results. 但是,我试图使过滤器返回0 /无/无结果时返回“ undefined,null,false / true”的条件。 It is possible? 有可能的?

I have something like that: 我有这样的事情:

filteredOffers.map(offer => {
  if(offer.name === "" || offer.name === null || offer.name === undefined) {
    console.log("Test test test")
  } else {
    return (
      <p>{offer.name}</p>
    )
  }
})

Your question is 你的问题是

Why console.log shows me nothing? 为什么console.log没有显示任何内容?

And in your question you say 在你的问题中你说

when the filter brings me no results, the console log do not print anything 当筛选器没有结果时,控制台日志不会打印任何内容

Where is what is happening. 发生了什么事。

.filter will return a new array with only the elements of the array that have passed some condition and returned true . .filter将返回一个新数组,其中仅包含已通过某些条件并返回true的数组元素。 If no conditions are true, it will return an empty array ( [] ). 如果没有条件成立,它将返回一个空数组( [] )。

.map will loop throgh an array and return something in this new array, if it's an empty array, .map won't run an just return that empty array. .map将遍历一个数组并在此新数组中返回某些内容,如果它是一个空数组,则.map不会运行而仅返回该空数组。

When you say when the filter brings me no results it means that .filter returned an empty array and when you do [].map(...) , the function inside .map won't run and it will just return [] . 当您说过when the filter brings me no results ,意味着.filter返回一个空数组,当您执行[].map(...).map的函数将不会运行,而只会返回[]

That answers Why console.log shows me nothing? 答案Why console.log shows me nothing? , you are doing [].map(...) and the function never runs. ,您正在执行[].map(...) ,该函数将永远无法运行。

testing 测试

!offer.name

will return true when 何时将返回true

  • offer.name is undefined offer.name未定义
  • offer.name is null offer.name为空
  • offer.name is false offer.name为假
  • offer.name is the empty string ('') offer.name是空字符串('')
  • offer.name is 0 offer.name为0

so you might want to filter your offers as follow 因此,您可能需要按照以下条件过滤报价

filteredOffers.filter(offer => !!offer.name)

and only then map the output 然后映射输出

filteredOffers.filter(offer => !!offer.name).map(offer => (<p>{offer.name}</p>))

暂无
暂无

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

相关问题 为什么 console.log 显示 html 文档中有 0 个加载的图像? - Why console.log shows me that there are 0 loaded images in the html document? console.log()没有任何显示 - console.log() nothing shows up console.log在FF附加生成器中不显示任何内容 - console.log shows nothing in FF add-on builder ajax请求可在控制台中访问的jsonp文件,但console.log(data)时未显示任何内容 - ajax request jsonp file accessible in console, but nothing shows up when console.log(data) &#39;TypeError: 无法读取未定义的属性 &#39;不正确&#39; 但 console.log 显示没有未定义的内容 - 'TypeError: Cannot read property 'incorrect' of undefined' but console.log shows there is nothing that is undefined 为什么我console.log一个对象,它显示对象,但是当我console.log时,它显示未定义 - Why I console.log a Object,it shows object,but when I console Object.value ,it shows undefined console.log在Liferay中的Firebug中什么都不做 - console.log does nothing in Firebug in Liferay Console.log显示隐藏的对象信息 - Console.log shows hidden object info Javascript - 为什么 ISO8601 格式的时间在 console.log 中以两种不同的格式显示? - Javascript - Why time in ISO8601 format shows in 2 different formats at console.log? 为什么console.log只显示由0.1 + 0.2 = 0.30000000000000004产生的部分数字 - Why console.log shows only part of the number resulting from 0.1+0.2=0.30000000000000004
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM