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