gpt4 book ai didi

angularjs - 如何使用最小值和最大值过滤 AngularJS 中的数据?

转载 作者:行者123 更新时间:2023-12-01 13:50:57 25 4
gpt4 key购买 nike

我有以下数据:

$scope.events = [{
"date": "2015-01-01",
"name": "party"
}, {
"date": "2015-02-01",
"name": "birthday"
}, {
"date": "2015-03-01",
"name": "graduation"
}, {
"date": "2015-04-01",
"name": "wedding"
}];

并且用户可以选择一个日期范围,例如:

$scope.dateRange = [
"start": "2015-01-01",
"end": "2015-03-01"
];

在这种情况下,我只想显示用户选择的日期之间的数据,例如:

 $scope.selectedEvents = [{
"date": "2015-01-01",
"name": "party"
}, {
"date": "2015-02-01",
"name": "birthday"
}, {
"date": "2015-03-01",
"name": "graduation"
}];

在这种情况下,用户选择了一个包含数据的日期范围,但是如果用户选择了一个超出可用数据范围的日期范围,例如:

$scope.dateRange = [
"start": "2014-12-01",
"end": "2015-08-01"
];

然后我想返回以下内容:

$scope.events = [{
"date": "2014-12-01",
"name": ""
}, {
"date": "2015-01-01",
"name": "party"
}, {
"date": "2015-02-01",
"name": "birthday"
}, {
"date": "2015-03-01",
"name": "graduation"
}, {
"date": "2015-04-01",
"name": "wedding"
}, {
"date": "2015-05-01",
"name": ""
}, {
"date": "2015-06-01",
"name": ""
}, {
"date": "2015-07-01",
"name": ""
}, {
"date": "2015-08-01",
"name": ""
}];

最佳答案

以下应该让你开始.. 变量更改为非范围变量以进行简单的 js 测试

var dateRange = {
"start": "2015-04-01",
"end": "2015-07-01"
};

var startDate = new Date(dateRange.start),
endDate = new Date(dateRange.end);

var res = events.filter(function(item){
var date = new Date(item.date);
return date >= startDate && date <=endDate;
});

如果您想推断日期,请按日期对默认数据进行排序并检查第一个和最后一个值。然后创建一个循环以在过滤之前将其他项目推送到主数组

DEMO

关于angularjs - 如何使用最小值和最大值过滤 AngularJS 中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31655868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com