在通过多个ajax异步调用推送它们时,从数组输出值时遇到麻烦。
我可以在控制台日志中输出主数组,但不能打印出我无法打印的单个值。我也想将值从高到低排序
$(document).ready(function() {
var callsArray = [];
$.ajax({
type: 'GET',
url: "js/equity-one.json",
dataType: "json",
traditional: true,
async: true,
success: function(data) {
$.each(data, function(i, item) {
callsArray.push(item);
$(".equity").append("<tr><td>" + item + "</td></tr>");
});
}
});
$.ajax({
type: 'GET',
url: "js/equity-two.json",
dataType: "json",
traditional: true,
async: true,
success: function(data) {
$.each(data, function(i, item) {
callsArray.push(item);
$(".equity").append("<tr><td>" + item + "</td></tr>");
});
}
});
$.ajax({
type: 'GET',
url: "js/equity-three.json",
dataType: "json",
traditional: true,
async: true,
success: function(data) {
$.each(data, function(i, item) {
callsArray.push(item);
$(".equity").append("<tr><td>" + item + "</td></tr>");
});
}
});
console.log(callsArray); // This outputs what has been pushed
var arr = callsArray.sort();
for (let x = 0; x < arr.length; x++) { //Not sure what to do here
console.log(arr[x]);
}
});
JSON示例
{"equity" : 5000} //File 1
{"equity" : 2000} //File 2
{"equity" : 10000} //File 3