javascript - 每个带有提要数组的 Javascript JSON
问题描述
在我的示例中,我试图获取ID和从数组feedUrls描述,但是获取每个提要的顺序不匹配。
如下所示,结果不是我的数组中从第一个到最后一个提要,尽管提要列在从 001 到 005的数组feedUrls中。
var feedUrls = ["https://api.myjson.com/bins/nkvdl", "https://api.myjson.com/bins/13wd2h", "https://api.myjson.com/bins/1b1kbt", "https://api.myjson.com/bins/10zc7d", "https://api.myjson.com/bins/60sqx"];
var arrayData1 = [];
var arrayData2 = [];
var getJSON = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status === 200) {
callback(null, xhr.response);
} else {
callback(status, xhr.response);
}
};
xhr.send();
};
feedUrls.forEach(function(entry) {
getJSON(entry,
function(err, data) {
if (err !== null) {
alert('Something went wrong: ' + err);
} else {
try {
arrayData1.push("<li>" + data[0].id + "</li>");
} catch (e) {}
document.getElementById('listId1').innerHTML = arrayData1.join("");
}
});
});
feedUrls.forEach(function(entry) {
getJSON(entry,
function(err, data) {
if (err !== null) {
alert('Something went wrong: ' + err);
} else {
try {
arrayData2.push("<li>" + data[0].title + "</li>");
} catch (e) {}
document.getElementById('listId2').innerHTML = arrayData2.join("");
}
});
});
ul {
display: inline-block;
}
<ul id="listId1">empty</ul>
<ul id="listId2">empty</ul>
结果:
001
005
004
003
002
title 001
title 004
title 002
title 005
title 003
预期的:
001
002
003
004
005
title 001
title 002
title 003
title 004
title 005
解决方案
forEach()
为回调提供一个索引,您可以使用它来按预期顺序收集结果,例如
var feedUrls = ["https://api.myjson.com/bins/nkvdl", "https://api.myjson.com/bins/13wd2h", "https://api.myjson.com/bins/1b1kbt", "https://api.myjson.com/bins/10zc7d", "https://api.myjson.com/bins/60sqx"];
var arrayData1 = [];
var arrayData2 = [];
var getJSON = function(url, callback, index) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status === 200) {
callback(null, xhr.response, index);
} else {
callback(status, xhr.response, index);
}
};
xhr.send();
};
feedUrls.forEach(function(entry, index) {
getJSON(entry,
function(err, data, index) {
if (err !== null) {
alert('Something went wrong: ' + err);
} else {
try {
arrayData1[index]="<li>" + data[0].id + "</li>";
} catch (e) {}
document.getElementById('listId1').innerHTML = arrayData1.join("");
}
},index);
});
feedUrls.forEach(function(entry, index) {
getJSON(entry,
function(err, data) {
if (err !== null) {
alert('Something went wrong: ' + err);
} else {
try {
arrayData2[index]="<li>" + data[0].title + "</li>";
} catch (e) {}
document.getElementById('listId2').innerHTML = arrayData2.join("");
}
},index);
});
ul {
display: inline-block;
}
<ul id="listId1">empty</ul>
<ul id="listId2">empty</ul>
新部分是index
变量。
否则评论所说的:你有许多独立的 XHR-s,它们的完成顺序不能保证。
推荐阅读
- git - 如何设置 ssh 密钥以从不同用户下的一台机器访问 github
- java - 将 SLF4J 秒表记录到不同的文件
- c++ - 错误:没有匹配函数调用 'std::basic_ifstream
::open(std::__cxx11::string&, const openmode&)' - node.js - 我无法使用节点 js 将视频上传到 youtube
- angular - Angular Material UI - 自动完成以最初保留现有的表单字段值
- powerbi - 如何在DAX中的SUMX函数中按两个相关表进行过滤
- python - Python中的反向引用
- javascript - 在父级上设置操作时,复选框样式未正确切换,更新的数据从父级发送到子级
- ansible - 通过 ansible AWX 在 ovirt_disk 模块中出错
- phpstan - 如何表示一个非空数组?