简体   繁体   中英

Why this find function return undefined?

Why this find function return 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.

Because empty strings are falsey , so the item.test conditions fails and excludes the item.

payload.test is an empty string so if(payload.test) return false

you can change it to console.log(payload.find(item => item.test || item.test === ''));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM