我对下面的代码有问题。我试图返回两个日期之间的所有投票,但由于某种原因,它不能很好地工作。
FirstShiftStart date ->2022-12-21 8:0:0
FirstShiftEnd date ->2022-12-21 14:0:0
let current_datetime = new Date();
var firstShiftStart = current_datetime.getFullYear() + "-" + (current_datetime.getMonth() + 1) + "-" + current_datetime.getDate() + " " + 08 + ":" + 00 + ":" + 00;
var firstShiftEnd = current_datetime.getFullYear() + "-" + (current_datetime.getMonth() + 1) + "-" + current_datetime.getDate() + " " + 14 + ":" + 00 + ":" + 00;;
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = yyyy + '-' + mm + '-' + dd;
votesforDay只返回今天的记录(所有数据都可以)。
但是,最后一行返回的是空数组
var votesforDay = dataAllJson.filter(x => x.Created >= today);
int likesFirstShift = votesforDay.filter(x => x.Created >= firstShiftStart && x.Created <= firstShiftEnd && x.Choice == "Like").length;
int likesFirstShift = votesforDay.filter(x => x.Created >= firstShiftStart && x.Created <= firstShiftEnd && x.Choice == "Like").length;
Choice: "Like"
Created: "2022-12-21 07:50:04"
Id: 12123
如果不满足这些条件,代码的最后一行将不返回任何东西:
- 数组中特定
JSON
元素的Created
属性必须大于或等于firstShiftStart
(在您的代码中为2022-12-21 08:00:00
) - 同一个
JSON
元素的Created
属性必须小于或等于firstShiftEnd
(2022-12-21 14:00:00
,在你的代码中) 最后,同一个
JSON
的Choice
属性应该等于Like
因为我们只知道JSON
对象数组中的一个元素,我们可以看到其中一个条件不满足-时间戳在firstShiftStart
之前,并且您的过滤器将从数组中删除它。
还有,你的代码中有一些语法错误——前导零,双零,等等。
我擅自修改了一点(不是全部),并在最后一行更改了过滤,以便您可以更好地理解为什么您的代码没有返回任何内容。
let current_datetime = new Date();
var firstShiftStart = current_datetime.getFullYear() + "-" + (current_datetime.getMonth() + 1) + "-" + current_datetime.getDate() + " 08:00:00";
var firstShiftEnd = current_datetime.getFullYear() + "-" + (current_datetime.getMonth() + 1) + "-" + current_datetime.getDate() + " 14:00:00";
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = yyyy + '-' + mm + '-' + dd;
var dataAllJson = [
{
"Choice": "Like",
"Created": "2022-12-22 11:50:04",
"Id": 1
},
{
"Choice": "dislike",
"Created": "2022-12-22 11:44:04",
"Id": 2
},
{
"Choice": "dislike",
"Created": "2022-12-22 07:50:04",
"Id": 3
},
{
"Choice": "dislike",
"Created": "2022-12-22 07:50:04",
"Id": 4
},
{
"Choice": "dislike",
"Created": "2022-12-22 07:50:04",
"Id": 5
},
{
"Choice": "Like",
"Created": "2022-12-22 07:50:04",
"Id": 6
},
];
var votesforDay = dataAllJson.filter(x => x.Created >= today);
var likesFirstShift = votesforDay.filter(function(x) {
if(x.Created >= firstShiftStart && x.Created <= firstShiftEnd && x.Choice == "Like") {
console.log(x.Id);
console.log(x.Created + " >= " + firstShiftStart);
console.log(x.Created + " <= " + firstShiftEnd);
console.log(x.Choice + " == Like");
}
return x.Created >= firstShiftStart && x.Created <= firstShiftEnd && x.Choice == "Like"
}).length;
console.log("=============================");
console.log(likesFirstShift);
注意事项:
firstShiftStart
和firstShiftEnd
的定义略有不同。请注意示例与代码 中的差异。- 我已将日期更改为今天(2022年12月22日),以便您可以测试代码
- 我还用半随机数据填充了
dataAllJson
,再次,用于测试目的 votesForDay.filter(...)
已扩展为一个略有不同的符号,用于测试目的(注意console.log
)