我正在尝试创建一个过滤器,该过滤器仅返回大于StartDate且小于与customDate进行比较的EndDate的结果。此Array填充表。到目前为止,我没有运气,并尝试了150种方式。有人可以放一点光吗?感谢。
打字稿:
this.evalSchedule = this.evalSchedule.filter(function(x) {
x.filter(function (v){
( new Date(v.customDate) > new Date(start) && new Date(v.customDate) < new Date(end) );
})
});
答案 0 :(得分:0)
我不容易理解您的数据结构是什么样的。但是你的代码有一个简单的问题:
this.evalSchedule.filter(function(x) {
// inside filter #1
x.filter(function (v){
// inside filter #2
console.log(v.customDate);
return v.customDate > new Date(start) && new Date(v.customDate) < new Date(end) // return statement for filter #2
})
// in this moment x contains only elements which passed filter #2
// now, this is problematic place
// -> missing return statement for filter #1
// -> it returns nothing so as default it will return undefined which will evaluate to false
// -> filter #1 should filter out all elements of evalSchedule
});
对于您的特定数据结构,下面给出的代码应该有效。但是正如@MikeMcCaughan指出的那样,我们需要额外的函数parseDate
来正确处理日期字符串in this topic:
function parseDate(input) {
var parts = input.split('/');
// new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])
return new Date(parts[0], parts[1]-1, parts[2]);
}
this.evalSchedule.map(function(x) {
return x.filter(function (v) {
console.log(v.customDate);
var customDateParsed = parseDate(v.customDate)
return customDateParsed > parseDate(start)
&& customDateParsed < parseDate(end);
})
});