Filtering Json Data by Current Date

Coder234

Pretty much just trying to display the next 3 current games by date. I would like to see the dates of the next 3 games and and have it account for a game if it is happening the day of (current date at the moment user is viewing).

$.getJSON('http://data.nba.com/data/v2015/json/mobile_teams/nba/2020/teams/trail_blazers_schedule_02.json', function(data) {

    var currentDate = new Date();

    $(data.gscd.g).each(function (index, value) {

        for(var i = 0; i < 1; i++){

            document.write('<h4>'+value.gdte+'</h4>');
        }
    });
});
ischenkodv

This can be achieved by making 3 operations: 1) filter games to remove past events, 2) sort games by date, 3) select 3 games. Something like this:

    $.getJSON('http://data.nba.com/data/v2015/json/mobile_teams/nba/2020/teams/trail_blazers_schedule_02.json', function(data) {
        var currentDate = new Date();

        // 1. filter
        var filtered = data.gscd.g.filter((entry) => Date.parse(entry.gdte) > currentDate);
        // 2. sort
        filtered.sort((a, b) => Date.parse(b.gdte) - Date.parse(b.gdte));
        // 3. get 3 games
        var result = filtered.slice(0,3)
    
        $(result).each(function (index, value) {
                document.write('<h4>'+value.gdte+'</h4>');
        });
    });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related