I have two arrays:
Matches
var matches = [
{ playerId: 1, matchId: 1 },
{ playerId: 2, matchId: 1 },
{ playerId: 3, matchId: 2 }];
Players
var players= [
{ name: "Ronaldo", id: 1 },
{ name: "Messi", id: 2 },
{ name: "Zlatan" id: 3 }];
I want to list all players who played on match with matchId 1;
for (i = 0; i < matches.length; i++) {
if (matches[i].matchId == 1)
{
var p = matches[i].playerId;
//alert(p);
var currentName = getPlayersInfo(p);
alert(currentName);
}
}
function getPlayersInfo(id){
var curID = id;
var name;
for (i = 0; i < players.length; i++) {
if (players[i].id == curID) {
name = players[i].name;
}
}
return name;
}
If I have just alert(p); instead of that I call getPlayersInfo(p) than I am getting alert two times with id 1 and 2 and that works fine but with getPlayersInfo(p) first alert is Ronaldo and than I am getting alert Messi multiply times.
What I am doing wrong in getPlayersInfo() ?
players? Where is that defined?