我有一个我需要转换成可读数据的数组网页。我如何使用jQuery/Ajax对数组进行排序?如何用ajax填充数组?
这里是链接https://fcctop100.herokuapp.com/api/fccusers/top/recent
我有一个我需要转换成可读数据的数组网页。我如何使用jQuery/Ajax对数组进行排序?如何用ajax填充数组?
这里是链接https://fcctop100.herokuapp.com/api/fccusers/top/recent
取决于您希望通过对它们进行排序什么。
假设您想按用户名对它们进行排序,您可以使用this answer来解决您的问题。
在下面的示例中,我用上述函数按用户名对数据进行排序。
//This will sort your array
//srot function from https://stackoverflow.com/a/5503957/3591300
function SortByName(a, b){
var aName = a.username.toLowerCase();
var bName = b.username.toLowerCase();
return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}
$.get("https://fcctop100.herokuapp.com/api/fccusers/top/recent", function(data) {
//here we run the function to sort the array of data before transforming it to table
data.sort(SortByName);
var table = '<table>'
for (i = 0; i < data.length; i++) {
table += '<tr><td>' + data[i].alltime + '</td><td><img width=20 height=20 src="' + data[i].img + '"></td><td>' + data[i].lastUpdate + '</td><td>' + data[i].recent + '</td><td>' + data[i].username + '</td></tr>';
}
$('body').append(table);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
这很好,除了,对于这里未声明的i变量'for(i = 0; i
哦,快速。 –
则可以通过在函数传递给排序方法进行排序在JavaScript的阵列。 在函数中,根据2个元素的比较结果返回-1或1。以下示例依赖订单属性,根据需要进行修改。 aoSortedUrgencies是一个对象数组。
aoSortedUrgencies.sort(function (a, b) { return ((a.Order< b.Order) ? -1 : (a.Order> b.Order? 1 : 0)) });
排序后,你就可以调用它来创建一个JSON字符串传递到Ajax调用:
JSON.stringify(aoSortedUrgencies)
http://stackoverflow.com/questions/28911984/filtering-objects-from-external-array-to-create-a-list-using-jquery-ajax的可能的复制 - 和 –
您发送请求,接收数据,然后使用映射或减少或算法处理信息...这取决于你需要用它做什么,你想要完成什么... –