[英]JavaScript, Expected to return a value at the end of arrow function
I have this: 我有这个:
values.map(value => {
if (value.gse === gse) {
return value.y
}
})
I need to map then return value.y
only in the case of the if
statement that I put in the example. 仅在示例中放置了
if
语句的情况下,我才需要映射然后返回value.y
。 How can avoid the warning of Expected to return a value at the end of arrow function
, I mean, how should I correctly write that code? 如何避免在
Expected to return a value at the end of arrow function
“ Expected to return a value at the end of arrow function
的警告,我的意思是,我应该如何正确编写该代码? with a return this
in case value.gse !== gse
? return this
以防万一value.gse !== gse
?
您需要使用filter
,然后map
:
values.filter(value => values.gse === gse).map(value => value.y);
You can achieve this with reduce() . 您可以使用reduce()实现此目的。 Code will be like:
代码如下:
values.reduce((list, value) => {
if(values.gse === gse) list.push(value.y);
return list;
}, []);
Hope this helps :) 希望这可以帮助 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.