我有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();
我收到&#34;无效日期&#34;
答案 0 :(得分:1)
根据docs,如果至少有一个参数无法转换为
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();
&#13;
答案 1 :(得分:1)
您可以使用Array#reduce
并将日期作为字符串进行比较,而它是ISO 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 } }] },
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>');
&#13;
答案 2 :(得分:1)
您必须先将日期值转换为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();
希望有所帮助