Javascript 嵌套在for循环中的getJSON块仅在最后一次迭代中执行

Javascript 嵌套在for循环中的getJSON块仅在最后一次迭代中执行,javascript,jquery,Javascript,Jquery,我有以下javascript代码: $.getJSON(url, function(data) { //Make a call to an api with the a url. // Take the Response field of the json response. // This field contains objects var d = data['Response']; for(var i = 0; i

我有以下javascript代码:

$.getJSON(url, function(data) {     //Make a call to an api with the a url. 

    // Take the Response field of the json response. 
    // This field contains objects
    var d = data['Response'];             

    for(var i = 0; i < d.length; i++) {     // For each object
        var obj1 = d[i];                    
        var id = obj1['id'];            // Take the id of the object

        var url2 = endpoint+'id='+id                     

        // Make a new api call based on the id of the object
        $.getJSON(url2, function(obj2) {
            console.log(i)
        });
    }
});
$.getJSON(url,函数(数据){//使用url调用api。
//获取json响应的响应字段。
//此字段包含对象
var d=数据[‘响应’];
对于每个对象(var i=0;i
出于某种原因,这只打印
i
的最后一个值。尽管如此,当我在内部块外部和
块内部打印
I
的值时,它会打印
I
的所有值

我还尝试对
I
使用
let
而不是
var
,但它随机而非顺序地打印
I
的值。它还将某些值打印两次

为什么会这样?
如何使内部块打印for循环期间
I
所取的所有值?

我认为调用回调时是一个经典问题,如中所述 类似的问题:您是否尝试将匿名函数放入循环中? 而不是使用let

$.getJSON(url, function(data) {     //Make a call to an api with the a url. 
        var d = data['Response'];             //Take the Response field of the json response. This field contains objects

        for(var i = 0; i < d.length; i++) {     //For each object
          (function(i) { // protects i in an immediately called function 
          var obj1 = d[i];                    
            var id = obj1['id'];            //Take the id of the object

            var url2 = endpoint+'id='+id
            $.getJSON(url2, function(obj2) {                     //Make a new api call based on the id of the object
                console.log(i)
              });
           })(i);
        }
$.getJSON(url,函数(数据){//使用url调用api。
var d=data['Response'];//获取json响应的响应字段。此字段包含对象
对于每个对象(var i=0;i

}))

我认为调用回调是一个经典问题,如 类似的问题:您是否尝试将匿名函数放入循环中? 而不是使用let

$.getJSON(url, function(data) {     //Make a call to an api with the a url. 
        var d = data['Response'];             //Take the Response field of the json response. This field contains objects

        for(var i = 0; i < d.length; i++) {     //For each object
          (function(i) { // protects i in an immediately called function 
          var obj1 = d[i];                    
            var id = obj1['id'];            //Take the id of the object

            var url2 = endpoint+'id='+id
            $.getJSON(url2, function(obj2) {                     //Make a new api call based on the id of the object
                console.log(i)
              });
           })(i);
        }
$.getJSON(url,函数(数据){//使用url调用api。
var d=data['Response'];//获取json响应的响应字段。此字段包含对象
对于每个对象(var i=0;i

}))

你有没有检查过类似的问题:@BJohn是的,我用我在帖子中提到的
let
尝试了这个解决方案,但是它随机而非顺序地打印
I
的值。这可能是因为
getJSON
是异步的。您可能希望重构以在顶级对象中积累数据(按索引键控)阅读更多:你是否检查过类似的问题,所以:@BJohn是的,我用
let
尝试了解决方案,正如我在帖子中提到的,但它随机而非顺序地打印
I
的值。这可能是因为
getJSON
是异步的。您可能希望重构以将数据累积到顶级对象(由索引设置键)中,而不是立即输出数据。请阅读更多: