简体   繁体   中英

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. 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? with a return this in case value.gse !== gse ?

您需要使用filter ,然后map

values.filter(value => values.gse === gse).map(value => value.y);

You can achieve this with reduce() . Code will be like:

values.reduce((list, value) => {
    if(values.gse === gse) list.push(value.y);
    return list;
}, []);

Hope this helps :)

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