gpt4 book ai didi

javascript - 如何对API返回的json进行排序?

转载 作者:行者123 更新时间:2023-12-03 07:32:57 26 4
gpt4 key购买 nike

这段 JavaScript 代码调用了Spotify API,并返回一个包含给定艺术家的所有专辑的表格。该表中的第五列包含一个名为流行度的值。我需要按第五列对表格进行排序,并根据流行度值打印出前 5 行(专辑)。

$(document).ready(function () {
$("#searchbutton").click(function () {
search();
});

function search() {
var query1 = document.getElementById('querybox').value;
$.get("https://api.spotify.com/v1/search?q=" + query1 + "&type=artist", function (data) {
//alert(data.artists.items[0].id);
getSeveralAlbums(data.artists.items[0].id);
});
}

function getSeveralAlbums(artistid) {
//alert(artistid);
$.getJSON("https://api.spotify.com/v1/artists/" + artistid + "/albums?album_type=album",
function (json) {
//bob = json;
//console.log(json);
for (var i = 0; i < json.items.length; i++) {
getAlbumInfo(json.items[i].href);
//console.log(json.items[i].href);
}
});
}

function getAlbumInfo(albumhref) {
//alert("a");
//console.log("a");
$(document).ready(function () {
$.getJSON(albumhref,
function (json) {
var tr;
// i<json.length
// Sort the array first by popularity. And then create a for loop and print the first five.
tr = $('<tr/>');
tr.append("<td>" + json.name + "</td>"); // Album Name
tr.append("<td>" + json.artists[0].name + "</td>"); // Artist Name
tr.append("<td>" + json.release_date + "</td>"); // Release Date
tr.append("<td>" + json.tracks.total + "</td>"); // Number of Tracks
tr.append("<td>" + json.popularity + "</td>"); // Popularity
$('table').append(tr);
//alert("table compiled");
//alert("Table done");
});
});
}
});

所以我的问题是。获得前 5 名专辑的最有效方法是什么?我已经有了数据,我只需要呈现该数据的特定部分。注意:包含所有艺术家专辑列表的 API 网址不包含所需的流行度数据。

尝试1:我尝试创建一个 2D 数组并将 albumhref(返回 json 的 API 链接,其中包含包含流行度的单个专辑数据)以及每个专辑的相应流行度存储在数组的单行中。然后根据受欢迎程度对数组进行排序,理想情况下也可以对 albumhref 进行排序。然后使用 for 循环运行 GET api 调用,该调用将从要排序的数组中获取前五个专辑的专辑数据。这不起作用。

Arrays returning undefined for API calls (spotify app)

<小时/>

Beyonce 专辑之一的示例 API 网址:

https://api.spotify.com/v1/albums/2UJwKSBUz6rtW4QLK74kQu

Beyonce 所有专辑列表的 API 网址:

https://api.spotify.com/v1/artists/6vWDO969PvNqNYHIOW5v0m/albums?album_type=album

最佳答案

下面的示例展示了获取艺术家的专辑列表,然后获取每个专辑的流行度值,最后对结果数组进行排序,因此最受欢迎的专辑是数组中的第一个专辑。

  $(document).ready(function () {
var albums = [], results = [];

// We start with calling the artist's api
$.ajax({
url: "https://api.spotify.com/v1/artists/6vWDO969PvNqNYHIOW5v0m/albums?album_type=album",
dataType: 'json',
success: function (data) {

// Extract the album name and href for the api call
albums = data.items.map(function (album) {
return {
name: album.name,
href: album.href
};
});

// Create an array of deferred calls
var deferreds = albums.map(function (album) {
var ajax = $.ajax({
url: album.href,
dataType: 'json',
success: function (data) {
// We only care about popularity and name
results.push({
popularity: data.popularity,
name: data.name
});
}
});

return ajax;
});

// Wait for the album calls to finish
$.when.apply($, deferreds).then(function() {

// Sort on popularity.
results.sort(function (a, b) {
if (a.popularity < b.popularity)
return 1;
else if (a.popularity > b.popularity)
return -1;
else
return 0;
});

// Results now contains a list of all albums, sorted on popularity
alert(results.length);
});
}
});
});

关于javascript - 如何对API返回的json进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35741093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com