[英]Why this find function return undefined?
Why this find function return undefined?为什么这个 find function 返回 undefined?
let payload = [{ test: '' }]; console.log(payload.find(item => item.test && item.test === '')); // undefined console.log(payload.find(item => item.test === '')); // {test: ''}
if item.test
is true, I think that it should test whether item.test
equal to ''.But it is not display like I think.如果
item.test
为真,我认为它应该测试item.test
是否等于''。但它不像我想的那样显示。
Because empty strings are falsey , so the item.test
conditions fails and excludes the item.因为空字符串是falsey ,所以
item.test
条件失败并排除该项目。
payload.test is an empty string so if(payload.test)
return false payload.test 是一个空字符串,所以
if(payload.test)
返回 false
you can change it to console.log(payload.find(item => item.test || item.test === ''));
您可以将其更改为
console.log(payload.find(item => item.test || item.test === ''));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.