简体   繁体   English

JavaScript,预期在箭头函数的末尾返回一个值

[英]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 functionExpected 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.

相关问题 预期在react中的箭头函数末尾返回一个值 - Expected to return a value at the end of arrow function in react 重构:期望在箭头函数的末尾返回一个值 - Refactor: Expected to return a value at the end of arrow function 预计在箭头 function 的末尾使用 if 语句返回一个值 - Expected to return a value at the end of arrow function with if statement 错误预期在箭头函数的末尾返回一个值 - Error Expected to return a value at the end of arrow function 预期在箭头函数错误的末尾返回一个值 - Expected to return a value at the end of arrow function error 预期在箭头函数结束时返回一个值:array-callback-return - Expected to return a value at the end of arrow function: array-callback-return 预期在箭头函数的结尾处返回一个值。 (一致返回) - Expected to return a value at the end of arrow function. (consistent-return) 预期在箭头函数array-callback-return的末尾返回一个值 - Expected to return a value at the end of arrow function array-callback-return 预期在箭头函数的末尾返回一个值consistent-return - Expected to return a value at the end of arrow function consistent-return 预期在 reactjs 上的箭头函数末尾返回一个值如何修复此错误? - Expected to return a value at the end of arrow function on reactjs how to fix this error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM