I am trying to 'join' two arrays on a key value.
The first array:
"matches": [
{
"name": 33,
"home_team": 4,
"away_team": 1,
"home_result": 3,
"away_result": 0
},
{
...
}
]
The second array:
"teams": [
{
"id": 1,
"name": "Russia"
},
{
...
}
]
When looping through matches, I would like to join the home_team id with the respective team id and display teams[i].name instead of the id. The same for away_team.
I tried the following but it does not work:
var mymatches = [];
for (i=0;i<matches.length;i++) {
var h = matches[i].home_team;
var a = matches[i].away_team;
var mymatch = {
name: matches[i].name,
home_team: teams[h].name,
away_team: teams[a].name
};
mymatches.push(mymatch);
}
When loggin variables a and h, the correct number is being output, however it is not being accepted in the [] part, which I find strange as it works with [i] in a for loop...
The full data is from a JSON file available below. I know that the matches array is nested within a "groups" object, however I was able to 'unnest' the "matches" arrays:
https://github.com/lsv/fifa-worldcup-2018/blob/master/data.json