1

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 :-)

6
  • 8
    You should push into goalkeepers array, not reassign a new array at each step of the loop. Commented Jul 21, 2015 at 14:11
  • 1
    You keep reassigning goalkepeers(sic) to a new array on each iteration through the loop. Presumably you meant to create an array and push a new object into the array? Commented Jul 21, 2015 at 14:12
  • What he said, but in code: goalkepeers .push(your json) Commented Jul 21, 2015 at 14:12
  • Try goalkepeers.push({"playerID": user.playerID,"playerName":user.playerName}) you are overwriting the array with each iteration. Commented Jul 21, 2015 at 14:14
  • ridiculous how many answers can't even use proper indentation. Commented Jul 21, 2015 at 14:18

3 Answers 3

2

Try this, Declare goalkeepers globally and then declare goalkeeper everytime in the loop and push it into goalkeepers array:

var goalkeepers = [];
for (var i = 0; i < json_parsed.Users.length; i++){  
             var user = json_parsed.Users[i];
             if(user.position=="GK"){
                var goalkeeper= {
                "playerID": user.playerID,
                 "playerName":user.playerName,
                     };
                goalkeepers.push(goalkeeper);
               }
}  
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot rohit, but i get an error in "aaData": goalkepeers, , it says Uncaught ReferenceError: goalkeepers is not defined. but i can see it being defined at line 1.
Sorry , my bad , it worked :-) Thanks a lot , appreciate :-)
Glad to help! otherwise i was also about to post any other approach to achieve the same :) .
1

it happens because you're replacing the values:

var goalkepeers= [];
for (var i = 0; i < json_parsed.Users.length; i++){  
    var user = json_parsed.Users[i];
    if(user.position=="GK"){
        var obj={
            "playerID": user.playerID,
            "playerName":user.playerName,
            };
        goalkeepers.push(obj);
        }
    }  

2 Comments

Thanks a lot Amin, but i get an error in "aaData": goalkepeers, , it says Uncaught ReferenceError: goalkeepers is not defined. but i can see it being defined at line 1.
Sorry , my bad , it worked :-) Thanks a lot , appreciate :-)
0

define the array before the loop

goldkeepers= array();

then inside the loop just do

goalkepeers.push({
        "playerID": user.playerID,
        "playerName": user.playerName,
    });

in your code your array is overwritten by the next json_parsed.Users element every time so the final result is an array with just the last element

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.