Array has values, but its length is zero [closed]

1

I need to iterate over this array , however, although it is not empty, length of it returns 0 as you can see, does anyone know the reason?

Thescenarioisasfollows:I'mgettingJSONdatafromanAPIanditeratingovertheobjectstoputthevaluesofthepropertyIwantinthissinglearray.Ihavetoiterateoveritnow,howeverwithsize0itbrokelogic.

//apkikey//base_urlvartags=[];varkeywordsId=[];functiontagsData(){varinput_tags=$('.chips').material_chip('data');input_tags.forEach(function(tag){tags.push(tag.tag);});}functionfindKeyWordId(){tags.forEach(function(tag){varurl=base_url+'search/keyword'+'?api_key='+apikey+'&query='+tag+'&page=1';$.getJSON(url,function(response){response.results.forEach(function(tag_obj){keywordsId.push(tag_obj.id);});});});console.log(keywordsId);};functionsearchMovies(){tagsData();findKeyWordId();console.log("Tamanho do array: " +  keywordsId.length);

    keywordsId.map(function(id){
    var movie_url = base_url + 'keyword/' + id  + '/movies' + '?api_key=' + apikey;

    $.getJSON(movie_url, function(results){
      console.log(results);
    });

  });
}
    
asked by anonymous 30.09.2017 / 10:46

1 answer

0

I discovered what was causing a headache, it was nothing magical, but rather the fact that JavaScript is asynchronous, I was calling my callback functions at the wrong time so I had a misperception of what was happening. >     

30.09.2017 / 11:18