2016-04-25 6 views
0

Ich habe json wie folgt:das letzte Datum Rückkehr aus Reihe von Terminen

"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} 
    } 
    ] 

Ich versuche, das letzte Datum (jüngste) zu bekommen, so dass ich die dazugehörigen „Notizen“ drucken.

 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(); 

Ich bin „ungültig Datum“

Antwort

1

Gemäß der docs bekommen, Wenn mindestens eines der Argumente nicht auf eine number umgewandelt werden, ist das Ergebnis NaN Und new Date(NaN)wird Invalid Date

Die Datumszeichenfolge inumbrechenbeim Einschieben des Arrays. Wenn DateObject über Number übergeben wird, wird ein numerischer Wert zurückgegeben, der date darstellt.

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();

1

könnten Sie Array#reduce verwenden und das Datum als Zeichenfolge vergleichen, während es ein ISO date ist.

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>');

1

Sie müssen Ihre Datumswerte zu Date-Objekte umwandeln zuerst:

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(); 

Hoffe, es hilft

Verwandte Themen