Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何用多个api调用填充数组?_Javascript_Arrays_Scope_Spotify - Fatal编程技术网

Javascript 如何用多个api调用填充数组?

Javascript 如何用多个api调用填充数组?,javascript,arrays,scope,spotify,Javascript,Arrays,Scope,Spotify,我一直在想如何用对SpotifyAPI的连续调用来填充这个“艺术家”数组。我试图用用户保存的库中的每个艺术家填充“艺术家”数组。该调用的工作方式是,每次调用最多返回50个艺术家。然而,当我在这里填充数组,然后尝试将一个新的I值传递给getUserData函数时,我认为I值没有更新,出于某种原因,我的“艺术家”数组只填充了226,而测试用例的“总计”值是276。有人能帮我吗?我认为这可能是一个范围问题,但我不知道在哪里。提前谢谢你 var i = 0; function getUserData(a

我一直在想如何用对SpotifyAPI的连续调用来填充这个“艺术家”数组。我试图用用户保存的库中的每个艺术家填充“艺术家”数组。该调用的工作方式是,每次调用最多返回50个艺术家。然而,当我在这里填充数组,然后尝试将一个新的I值传递给getUserData函数时,我认为I值没有更新,出于某种原因,我的“艺术家”数组只填充了226,而测试用例的“总计”值是276。有人能帮我吗?我认为这可能是一个范围问题,但我不知道在哪里。提前谢谢你

var i = 0;
function getUserData(accessToken, i) {
    return $.ajax({
        url: 'https://api.spotify.com/v1/me/tracks?limit=50&offset=' + i,
        headers: {
           'Authorization': 'Bearer ' + accessToken
        }
    });
}

 var artists = [];
 loginButton.addEventListener('click', function() {
      login(function(accessToken) {


       getUserData(accessToken, i)
            .then(function(response) {
            var total = response.total;

            while(i < total) {
            i+=50;
             getUserData(accessToken, i)
            .then(function(response) {
                total = response.total;
                alert(total); //testing
                loginButton.style.display = 'none';

for (var j = 0; j < 50 && artists.length < total; j++){                         

    artists.push(response.items[j].track.album.artists[0].name);
}
         alert(artists[7] + artists.length); //testing
         alert(artists[artists.length - 1]); //testing
            });
            }  


            });



        });
});
var i=0;
函数getUserData(accessToken,i){
返回$.ajax({
网址:'https://api.spotify.com/v1/me/tracks?limit=50&offset="我,,
标题:{
“授权”:“承载人”+accessToken
}
});
}
var=[];
loginButton.addEventListener('click',function(){
登录(函数(accessToken){
getUserData(accessToken,i)
.然后(功能(响应){
var total=response.total;
而(i

谢谢你的帮助

while循环中的第一个
getUserData
i=50
开始-因此前50个不会被推入
artists
数组中

如上所述,但使用本机
Promise.all

loginButton.addEventListener('click', function() {
    login(function(accessToken) {
        loginButton.style.display = 'none';
        var arr = [getUserData(accessToken, i)];
        arr[0]
        .then(function(response) {
            for (var i = 50; i < response.total; i += 50) {
                arr.push(getUserData(accessToken, i));
            }
            Promise.all(arr).then(function(chunks) {
                var artists = [].concat.apply([], chunks.map(function(response) {
                    return response.items.map(function(item) {
                        return item.track.album.artists[0].name;
                    });
                }));
                // artists array is filled, now do what you need
            });
        })
        .catch(function(err) {
            // handle errors here
        });
    });
});
loginButton.addEventListener('click',function()){
登录(函数(accessToken){
loginButton.style.display='none';
var arr=[getUserData(accessToken,i)];
arr[0]
.然后(功能(响应){
对于(变量i=50;i
只是一个简单的猜测-你试过了吗
var artists = [];
loginButton.addEventListener('click', function() {
    login(function(accessToken) {
        var i = 0;
        getUserData(accessToken, i)
        .then(function(response) {
            var total = response.total;
            // added code to store the first 50 artists
            for (var j = 0; j < 50 && artists.length < total; j++) {
                artists.push(response.items[j].track.album.artists[0].name);
            }
            // end added code
            while (i < total) {
                i += 50;
                getUserData(accessToken, i)
                .then(function(response) {
                    total = response.total;
                    alert(total); //testing
                    loginButton.style.display = 'none';

                    for (var j = 0; j < 50 && artists.length < total; j++) {
                        artists.push(response.items[j].track.album.artists[0].name);
                    }
                    alert(artists[7] + artists.length); //testing
                    alert(artists[artists.length - 1]); //testing
                });
            }
        });
    });
});
loginButton.addEventListener('click', function() {
    login(function(accessToken) {
        loginButton.style.display = 'none';
        var arr = [getUserData(accessToken, i)];
        arr[0]
        .then(function(response) {
            for (var i = 50; i < response.total; i += 50) {
                arr.push(getUserData(accessToken, i));
            }
            $.when.apply($, arr)
            .then(function() {
                var artists = [].concat.apply([], [].map.call(arguments, function(response) {
                    return response.items.map(function(item) {
                        return item.track.album.artists[0].name;
                    });
                }));
                // artists array is filled, now do what you need
            });
        })
        .catch(function(err) {
            // handle errors here
        });
    });
});
loginButton.addEventListener('click', function() {
    login(function(accessToken) {
        loginButton.style.display = 'none';
        var arr = [getUserData(accessToken, i)];
        arr[0]
        .then(function(response) {
            for (var i = 50; i < response.total; i += 50) {
                arr.push(getUserData(accessToken, i));
            }
            Promise.all(arr).then(function(chunks) {
                var artists = [].concat.apply([], chunks.map(function(response) {
                    return response.items.map(function(item) {
                        return item.track.album.artists[0].name;
                    });
                }));
                // artists array is filled, now do what you need
            });
        })
        .catch(function(err) {
            // handle errors here
        });
    });
});