javascript - 内部循环条件与nodejs不匹配
问题描述
内部循环条件未得到满足请指导如何得到这个以及是什么原因造成的?
var inputDate = '2020-04-11';
var result = []
for (var l = 0; l < reports.length; l++) {
let myReport = {}
myReport = await req.models.myReports.findAll({
where: {
id: reports[l].myReportId
}
})
// I am getting values in myReport[0].lastDate like : '2019-04-10', '2020-01-01', '2024-05-12'
// As i can see last last value is greater then inputDate it should not be went through if condition that mean result should stay blank but it does allow don't know why
if ((inputDate > myReport[0].lastDate)) {
result.push(myReport[0].name)
}
}
解决方案
由于 inputDate 变量是一个字符串,并且您可能将一个字符串与另一个字符串进行比较,这是一个字符串比较,而不是日期,我会首先将您的字符串解析为日期,然后比较它们。
var inputDate = Date.parse('2020-04-11');
var result = []
for (var l = 0; l < reports.length; l++) {
let myReport = {}
myReport = await req.models.myReports.findAll({
where: {
id: reports[l].myReportId
}
})
// I am getting values in myReport[0].lastDate like : '2019-04-10', '2020-01-01', '2024-05-12'
// As i can see last last value is greater then inputDate it should not be went through if condition that mean result should stay blank but it does allow don't know why
if ((inputDate > Date.parse(myReport[0].lastDate))) {
result.push(myReport[0].name)
}
}
推荐阅读
- java - 如何在java中打印星形图案?
- sql - Teradata - 在每个表上运行查询,返回相同的结果集
- python-3.x - 如何计算python中两个二面角(周期)角分布之间的距离?
- python - 是否可以从另一个类访问一个类的文本框条目(并附加到它)?在这种情况下,它将来自 Page2
- prolog - 无限递归错误和条件?
- android-canvas - 如何从 FirebaseStorage 检索图像并将其显示在 Canvas 中?
- mysql - 按ID分组时如何获得最频繁的值?
- wordpress - WordPress。在某些帖子中添加一个词到永久链接
- python - 从传感器到网站或移动应用程序的数据
- python - 能够为 Project Euler #8 找到最大的产品,但是产生这个最大产品的数字是多少?