从日期数组中返回最新日期
Return the latest date from array of dates
我有如下json:
"reviews":[
{
"notes": "Great place, perfect location but the best part was the Staff. Thank you so much for making our trip so wonderful!",
"date": "2015-04-18",
"rating":{"overall": 100}
},
{
"notes": "I messed up the days I was supposed to be there but they did everything they could to get me a room, which I did. The guy at the front was super nice and then drew me an entire map based off of how much time I had there in Rome. It was extremely helpful and the restaurant he recommends (a friends who you get 10% off of) had good lasagna. Great place for a really reasonable price. Also, the doors do not automatically lock for you room, so just remember to lock your door when you leave. ",
"date": "2016-04-18",
"rating":{"overall": 94}
}
]
我正在尝试获取最新日期(最新日期),以便我可以打印相关的"注释"。
function highestReview() {
var maxNumb = [];
for(var y = 0; y < data.reviews.length; y++){
maxNumb.push(data.reviews[y].date);
}
var latestDate = new Date(Math.max.apply(null, maxNumb));
console.log(latestDate);
}
highestReview();
我收到"无效日期"
在根据文档,如果至少一个参数无法转换为
number
,则结果为NaN
并且new Date(NaN)
将被Invalid Date
推送数组时将日期字符串包装在new Date
中。当DateObject
通过Number
传递时,将返回表示date
的数值。
var data = {
"reviews": [{
"notes": "Great place, perfect location but the best part was the Staff. Thank you so much for making our trip so wonderful!",
"date": "2015-04-18",
"rating": {
"overall": 100
}
}, {
"notes": "I messed up the days I was supposed to be there but they did everything they could to get me a room, which I did. The guy at the front was super nice and then drew me an entire map based off of how much time I had there in Rome. It was extremely helpful and the restaurant he recommends (a friends who you get 10% off of) had good lasagna. Great place for a really reasonable price. Also, the doors do not automatically lock for you room, so just remember to lock your door when you leave. ",
"date": "2016-04-18",
"rating": {
"overall": 94
}
}]
};
function highestReview() {
var maxNumb = [];
for (var y = 0; y < data.reviews.length; y++) {
maxNumb.push(new Date(data.reviews[y].date));
}
var latestDate = new Date(Math.max.apply(null, maxNumb));
console.log(latestDate);
}
highestReview();
您可以使用
Array#reduce
并将日期作为字符串进行比较,而它是一个 ISO 日期。
var data = { "reviews": [{ "notes": "Great place, perfect location but the best part was the Staff. Thank you so much for making our trip so wonderful!", "date": "2015-04-18", "rating": { "overall": 100 } }, { "notes": "I messed up the days I was supposed to be there but they did everything they could to get me a room, which I did. The guy at the front was super nice and then drew me an entire map based off of how much time I had there in Rome. It was extremely helpful and the restaurant he recommends (a friends who you get 10% off of) had good lasagna. Great place for a really reasonable price. Also, the doors do not automatically lock for you room, so just remember to lock your door when you leave. ", "date": "2016-04-18", "rating": { "overall": 94 } }] },
latestDate = data.reviews.reduce(function (r, a, i) {
return !i || a.date > r ? a.date : r;
}, undefined);
document.write('<pre>' + JSON.stringify(latestDate, 0, 4) + '</pre>');
必须先将日期值转换为 Date 对象:
function highestReview() {
var maxNumb = [];
for(var y = 0; y < data.reviews.length; y++){
maxNumb.push(new Date(data.reviews[y].date));
}
var latestDate = new Date(Math.max.apply(null, maxNumb));
console.log(latestDate);
}
highestReview();
希望对你有帮助
相关文章:
- 比较从函数和生成的日期对象
- Json数据包含日期和时间格式
- Javascript日期格式类似于ISO,但本地
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- 如何在JavaScript中延长当前日期
- jQuery-2.1.1.min.js或最新版本jQuery-2.13.min.js不会't支持'@
- 两位数的月份日期验证
- 更改angularjs中的日期-时间格式
- addData()从最新图表中删除.js 2.1.3-怎么了
- 将日期时间作为 JSON 发送将无法在我的视图中正确显示
- 将日期时间从json转换为可读格式
- 从日期数组中返回最新日期
- 从客户端的对象数组中获取最新日期的优雅方法是什么
- javascript foreach on array / json data 但按最新日期排序
- 你如何获得当前日期's的开始时间和最新时间(以毫秒为单位)
- MongoDB在集合中查找最新日期
- 读取appache清单值以获取最新更新日期
- 使用jquery,根据日期按时间顺序排序JSON,最新的在前
- 如何从节点组中查找最新的元素,其中元素的属性表示日期和时间.在xml中
- Javascript -最新日期和最高优先级