3
关注
1627
浏览

为什么过滤器不能提供与迭代一样的结果?

为什么被折叠? 0 个回复被折叠
forkun 未验证用户 用户来自于: 广东省
2020-09-17 01:46

删除{}

const test2 = JSON_LOCS.filter(loc => { // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter 
    loc.city.includes('las'); 
}); 

const test2 = JSON_LOCS.filter(loc => // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter 
    loc.city.includes('las'); // When not wrapped into {} it assumes its the return statement 
); 
billy 未验证用户 用户来自于: 广东省
2020-09-16 11:52

代替此行

oc.city.includes('las'); 

写这条线

return oc.city.includes('las'); 

你只是忘记了这一步的,在这种情况下,将返回undefined

关于作者

问题动态

发布时间
2020-09-15 15:51
更新时间
2022-09-15 16:04
关注人数
3 人关注
个人工作笔记 Powered BY WeCenter V4.1.0 © 2024 粤ICP备2020123311号