简体   繁体   English

预期在箭头函数结束时返回一个值:array-callback-return

[英]Expected to return a value at the end of arrow function: array-callback-return

I have a couple of actions on a function to extract unix time at midnight, I map through the array and then filter by those values that modulus to 0. However I keep getting this pesky error msg, regardless of whether I return the map function or not.. Code below:我对一个函数执行了几个操作来在午夜提取 unix 时间,我映射数组,然后按那些模数为 0 的值进行过滤。但是我一直收到这个讨厌的错误消息,无论我是返回 map 函数还是不是..下面的代码:

  midnightFunction = arr => {
    return arr.map(v =>
      v.filter(t => {
        if (t.published_at % 86400 === 0) {
          return t;
        };
      }
      )
    );
  };

My array is an array and looks like this:我的数组是一个数组,看起来像这样:

[[{published_at: 1578960000, value: 20.41}, {published_at: 1578960000, value: 20.41},...etc ], [], []...], [[], [], []...] , ... etc

I want the result to have the same structure, but return just those values that match the condition.我希望结果具有相同的结构,但只返回与条件匹配的那些值。

t.published_at % 86400 === 0

The function you pass to array.filter() should always return a truthy or falsy value.您传递给array.filter()的函数应始终返回真值或假值。 In your current code, if t.published_at % 86400 is not zero, the function doesn't return any value (it returns undefined).在您当前的代码中,如果t.published_at % 86400不为零,则该函数不返回任何值(它返回 undefined)。 Additionally, if that value is zero, instead of returning something like true/false it returns the element itself.此外,如果该值零,而不是返回像真/假返回元素本身。

Your function should be as simple as this:您的功能应该像这样简单:

 const midnightFunction = arr => arr.map(v => v.filter(t => t.published_at % 86400 === 0)); //for demonstration let data = [[{published_at: 1578960000, value: 20.41}, {published_at: 1578970000, value: 23.21}],[{published_at: 1578873601, value: 14.01}, {published_at: 1578873600, value: 27.25}]]; console.log(midnightFunction(data));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 预期在箭头函数array-callback-return的末尾返回一个值 - Expected to return a value at the end of arrow function array-callback-return 预期在箭头函数array-callback-return中返回一个值 - Expected to return a value in arrow function array-callback-return React:箭头函数需要返回值array-callback-return - React: Arrow function expected a return value array-callback-return 期望在过滤函数的箭头函数array-callback-return结尾处返回一个值 - Expected to return a value at the end of arrow function array-callback-return on filter function 反应警告 预期在箭头函数数组-回调-返回结束时返回一个值 - react warning Expected to return a value at the end of arrow function array-callback-return 为什么我收到此警告 159:35 警告预计将在箭头 function 数组回调返回的末尾返回一个值? - Why i am getting this warning 159:35 warning Expected to return a value at the end of arrow function array-callback-return? 正确警告:“预期在箭头函数数组回调返回中返回一个值” - Correct warning : "Expected to return a value in arrow function array-callback-return " 如何修复“预期在箭头函数数组回调返回中返回一个值”? - How to fix "Expected to return a value in arrow function array-callback-return"? 如何修复警告“预计在箭头 function 数组回调返回中返回一个值” - How fix warning “Expected to return a value in arrow function array-callback-return” Array.prototype.filter() 期望在箭头函数 array-callback-return 结束时返回一个值 - Array.prototype.filter() expects a value to be returned at the end of arrow function array-callback-return
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM