i am trying to populate a table using datatables jquery library , the problem encountered is that only the last value is displayed ,the values are accessed by looping through data stored in json . Here is my code :-
for (var i = 0; i < json_parsed.Users.length; i++) {
var user = json_parsed.Users[i];
if (user.position == "GK") {
goalkepeers = [{
"playerID": user.playerID,
"playerName": user.playerName,
}];
}
}
$('#myTable').dataTable({
"aaData": goalkepeers,
"aoColumns": [{
"mDataProp": "playerID"
}, {
"mDataProp": "playerName"
},
]
});
Everything works fine but only one data is displayed in my table , the array should be like this
goalkepeers = [{
"playerID": player1ID,
"playerName": player1Name,
},
{
"playerID": player2ID,
"playerName": player2Name,
}];
Any help will be appreciated , Thanks again :-)
goalkeepersarray, not reassign a new array at each step of the loop.goalkepeers(sic) to a new array on each iteration through the loop. Presumably you meant to create an array andpusha new object into the array?goalkepeers .push(your json)goalkepeers.push({"playerID": user.playerID,"playerName":user.playerName})you are overwriting the array with each iteration.