title: Access array variables outside for loop javascript
body: <p>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 :-</p>
  
  <pre><code>for (var i = 0; i &lt; 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"
          },
  
      ]
  });
  </code></pre>
  
  <p>Everything works fine but only one data is displayed in my table , the array should be like this </p>
  
  <pre><code>goalkepeers = [{
      "playerID": player1ID,
      "playerName": player1Name,
  }, 
  {   
      "playerID": player2ID,
      "playerName": player2Name,
  }];
  </code></pre>
  
  <p>Any help will be appreciated , Thanks again :-) </p>