0

Im trying to populate four arrays with data from the ajax call but when I do a console.log I get a blank array. If I console log the data I get enter image description here

var progress = new Array();
var test = new Array();
var done = new Array();
var todoArray = new Array();

  function getTasks(todoArray){

    $.ajax({
  method: "GET",
  url: "/tasks/"
})
  .done(function(data) {

    for(var i in data){

      if (data[i].status == "todo") {
        todoArray.push(data[i]);
      }
      else if(data[i].status == "in-progress"){
        progress.push(data[i]);
      }
      else if(data[i].status == "to-test"){
        todoArray.push(task);    
      }
      else if(data[i].status == "done"){
        done.push(data[i]);
      }
    }
  });

  console.log(todoArray);
}

getTasks(todoArray);
user3882976
  • 105
  • 10
  • What do you mean is blank? I see two objects in there. – Garis M Suero Apr 29 '15 at 20:27
  • It's an async call... – tymeJV Apr 29 '15 at 20:27
  • What type of data are you getting ? I mean what does data hold ?? and your for loop doesn't make any sense ... you are using i as an index to data array while assigning it the values the data array holds ... It wil never get anything since the indexes will always be wrong... – Uzumaki Naruto Apr 29 '15 at 20:30
  • i console.logged data theres nothing in todoArray – user3882976 Apr 29 '15 at 20:32
  • See also: [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Felix Kling Apr 29 '15 at 20:39
  • Object {id: 1, title: "Test", body: "This is a test", project_id: 1, category_id: 1…} – user3882976 Apr 29 '15 at 20:50

0 Answers0