我在某个函数上有几个动作来提取午夜的unix时间,我通过数组进行映射,然后按将模数设为0的那些值进行过滤。但是,无论是否返回该错误,我都会不断收到此讨厌的错误消息。地图功能与否 下面的代码:
midnightFunction = arr => {
return arr.map(v =>
v.filter(t => {
if (t.published_at % 86400 === 0) {
return t;
};
}
)
);
};
我的数组是一个数组,看起来像这样:
[[{published_at: 1578960000, value: 20.41}, {published_at: 1578960000, value: 20.41},...etc ], [], []...], [[], [], []...] , ... etc
我希望结果具有相同的结构,但只返回那些与条件匹配的值。
t.published_at % 86400 === 0
答案 0 :(得分:1)
您传递给array.filter()
的函数应始终返回真实或虚假值。在当前代码中,如果t.published_at % 86400
不为零,则该函数不返回任何值(返回未定义)。此外,如果该值 为零,则不返回诸如true / false的值,而是返回元素本身。
您的功能应该像这样简单:
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));