按日期在2个日期之间过滤数组 [英] Filter Array in Array by date between 2 dates
问题描述
我正在尝试通过TypeScript中用户的日期/日期输入来过滤LineChart的数据数组,以用于我的Angular App。
数据数组具有以下结构:
I am trying to filter a data-array of a LineChart by the from-date / to-date input of a user in TypeScript for my Angular App. The data array has the following structure:
var multi = [
{
"name": "test1",
"series": [
{
"date": new Date("2018-01-01T01:10:00Z"),
"value": 44
},...
]
},
{
"name": "test2",
"series": [
{
"date": new Date("2018-01-01T01:10:00Z"),
"value": 38
},...
]
},
{
"name": "test3",
"series": [
{
"date": new Date("2018-01-01T01:10:00Z"),
"value": 33
},...
]
}
];
我现在想要按照内部日期在'之后'的条件过滤数组的项目fromDate'和'toDate'之前。我尝试了以下方法:
I now want to filter the items of the array by the criteria that the date inside is after a 'fromDate' and before a 'toDate'. I tried the following:
obj.forEach(data => {
console.log(data.name);
data.series = data.series.filter((item: any) => {
item.date.getTime() >= fromDate.getTime() &&
item.date.getTime() <= toDate.getTime();
});
});
obj []
数组为空之后 obj [i] .series
数组。有人可以帮我吗?迭代似乎是正确的,因为调试给了我所有日期,同样来自比较日期的真/假语句也是正确的。
the obj[]
array has an empty obj[i].series
array afterwards. Can anybody help me here? The iteration seems to be right since debugging gave me all the dates, also the true/False statements from the date comparing was right as well.
提前致谢
推荐答案
您需要 return
compairing值,显式
You need to return
the compairing value, either explicit
data.series = data.series.filter((item: any) => {
return item.date.getTime() >= fromDate.getTime() &&
item.date.getTime() <= toDate.getTime();
});
或没有括号,隐式。
data.series = data.series.filter((item: any) =>
item.date.getTime() >= fromDate.getTime() && item.date.getTime() <= toDate.getTime()
);
这篇关于按日期在2个日期之间过滤数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!