Reverse order of javascript associative array

Hi,

I’m working on a school project where I want to visualize all the matches of the Champions League. I already have a calendar that shows upcoming matches, but now I want to show the scores of the last 3 play dates.
To do this, I first wrote some code to get a Javascript array where the key is a date, and the value another array of all matches that are played on that particular date.

However, I can’t simple print them because I want the fixtures of the last 3 play dates (I still have to exclude the matches in the future, but that shouldn’t be a problem). I thought about using ‘reverse’ to reverse the array and then using a simple loop to print the scores. Unfortunately, .reverse() doesn’t work because it’s an associative array. How could I still reverse the array so the keys (dates) are displayed descending?

Many thanks in advance!

JavaScript doesn’t have associative arrays - when you use associative array notation in JavaScript you are working with objects.

Object properties in JavaScript don’t have any order so reversing the order they don’t have is meaningless.

If you want objects in JavaScript to have an order (and in JavaScript object properties can be treated as objects) then you should put them in an array.

1 Like

So there is no way to get only the last 3 dates besides using arrays? If not, could you tell me more about using arrays instead of objects? I declared my array like var fixtures = e[] and filled it with dates like this:
fixtures[y['date']] = [] where y[‘date’] is the value of the array I’m looping through (the array that contains the data of all fixtures). Then I looped through this fixtures array again, to compare the date with every date in another array that contains all the fixtures, and added the fixture to that fixtures array if the dates matched.

Not sure just what you are trying to do with that statement but see if the following helps;

fixtures.push(y['date']);

The below example may help a little. This was written with huge assumptions about the data that you are using.

var championship = [
    {
        date: '2016-01-01T13:00:00Z',
        awayTeamName: 'Team 1',
        homeTeamName: 'Team 2',
        result: {
            awayScore: 0,
            homeScore: 1
        }
    },
    {
        date: '2016-01-03T11:00:00Z',
        awayTeamName: 'Team 7',
        homeTeamName: 'Team 8',
        result: {
            awayScore: 0,
            homeScore: 0
        }
    },  
    {
        date: '2016-01-02T16:00:00Z',
        awayTeamName: 'Team 1',
        homeTeamName: 'Team 6',
        result: {
            awayScore: 1,
            homeScore: 1
        }
    },
    {
        date: '2016-01-03T12:30:00Z',
        awayTeamName: 'Team 9',
        homeTeamName: 'Team 10',
        result: {
            awayScore: 0,
            homeScore: 1
        }
    },    
    {
        date: '2016-01-01T13:00:00Z',
        awayTeamName: 'Team 3',
        homeTeamName: 'Team 4',
        result: {
            awayScore: 2,
            homeScore: 1
        }
    },
    {
        date: '2016-01-01T14:00:00Z',
        awayTeamName: 'Team 5',
        homeTeamName: 'Team 6',
        result: {
            awayScore: 0,
            homeScore: 2
        }
    }     
];

function lastPlayedDates(fixtures, days) {
    return fixtures.reduce(function (uniq, match) {
        if (uniq.indexOf(match.date) < 0 ) uniq.push(match.date);
        return uniq;
    }, []).sort().slice(0 - days);
}

function displayMatches(date) {
    var matches = championship.filter(function (match) { return match.date === date; });
    
    console.log(date);
    matches.forEach(displayResults);
};

function displayResults(match) {
    console.log(match.homeTeamName + ' (' + match.result.homeScore + ')-(' + match.result.awayScore + ') ' + match.awayTeamName);
}

lastPlayedDates(championship, 3).forEach(displayMatches);
1 Like

That really is a big help, thank you very much!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.