使用Javascript使用XHR对获取的API数据进行排序
我在FDA的药品API和XMLHttpRequest()之间有工作联系;。如何使用其通用名称(results[“open_fda”].GENERIC_NAME)对获取的数据进行升序排序 我无法从该API导入所有数组,即使我将其限制为100个条目,我认为这会适得其反 有什么想法吗 提前谢谢你使用Javascript使用XHR对获取的API数据进行排序,javascript,html,json,Javascript,Html,Json,我在FDA的药品API和XMLHttpRequest()之间有工作联系;。如何使用其通用名称(results[“open_fda”].GENERIC_NAME)对获取的数据进行升序排序 我无法从该API导入所有数组,即使我将其限制为100个条目,我认为这会适得其反 有什么想法吗 提前谢谢你 <script> var header = document.querySelector('header'); var section = document.queryS
<script>
var header = document.querySelector('header');
var section = document.querySelector('section');
var requestURL = 'https://api.fda.gov/drug/label.json?limit=100';
var request = new XMLHttpRequest();
request.open('GET', requestURL);
request.responseType = 'json';
request.send();
request.onload = function() {
var drugs = request.response;
populateHeader();
showDrugs(drugs);
}
function populateHeader() {
var myH1 = document.createElement('h1');
myH1.textContent = "Maria Health Prescription Drugs";
header.appendChild(myH1);
var myPara = document.createElement('p');
header.appendChild(myPara);
}
function showDrugs(jsonObj) {
var drug = jsonObj['results'];
for(i = 0; i < drug.length; i++) {
var myArticle = document.createElement('article');
var myH2 = document.createElement('h2');
var myPara1 = document.createElement('name');
var myPara2 = document.createElement('p');
var myPara3 = document.createElement('p');
var myPara4 = document.createElement('p');
var myPara5 = document.createElement('warning');
var myPara6 = document.createElement('p');
var myPara7 = document.createElement('p');
var myPara8 = document.createElement('p');
var myList = document.createElement('ul');
myH2.textContent = 'Generic Name: ' + drug[i]["openfda"].generic_name;
myPara2.textContent = 'Product Type: ' + drug[i]["openfda"].product_type;
myPara3.textContent = 'Purpose: ' + drug[i].purpose;
myPara4.textContent = 'Indication and Usage: ' + drug[i].indications_and_usage;
myPara5.textContent = drug[i].warnings;
myPara6.textContent = 'Active Ingredients: ' + drug[i].active_ingredient;
myPara7.textContent = 'Inactive Ingredients: ' + drug[i].inactive_ingredient;
myPara8.textContent = 'Storage and Handling: ' + drug[i].storage_and_handling;
myArticle.appendChild(myH2);
myArticle.appendChild(myPara1);
myArticle.appendChild(myPara2);
myArticle.appendChild(myPara3);
myArticle.appendChild(myPara4);
myArticle.appendChild(myPara5);
myArticle.appendChild(myPara6);
myArticle.appendChild(myPara7);
myArticle.appendChild(myPara8);
myArticle.appendChild(myList);
section.appendChild(myArticle);
}
}
</script>
</body>
</html>
var header=document.querySelector('header');
var section=document.querySelector('section');
var requestURL=https://api.fda.gov/drug/label.json?limit=100';
var request=new XMLHttpRequest();
打开('GET',requestURL);
request.responseType='json';
request.send();
request.onload=函数(){
var药物=请求。响应;
populateHeader();
毒品(毒品);;
}
函数populateHeader(){
var myH1=document.createElement('h1');
myH1.textContent=“玛丽亚健康处方药”;
表头.追加子项(myH1);
var myPara=document.createElement('p');
标题.appendChild(myPara);
}
功能展示药物(jsonObj){
var药物=jsonObj[‘结果’];
对于(i=0;i
我尝试了以下代码:
function showDrugs(jsonObj) {
var drug = jsonObj['results'];
drug.sort(function(a, b){
return a['openfda'].generic_name - b['openfda'].generic_name;
});
console.log(drug);
我也尝试过,得到了同样的结果:
function showDrugs(jsonObj) {
var drug = jsonObj['results'];
drug.sort(function(a, b){
return a.openfda.generic_name - b.openfda.generic_name;
});
console.log(drug);
它可以工作,但不按通用名称排序。相反,它只是改变了数据,但我不知道它用什么来分类
我发现了这个,但是我在如何使用它上遇到了问题
编辑:
解决了!用这个答案
//对JSON函数进行排序
药物分类(功能(a,b){
//返回a.openfda.generic_name-b.openfda.generic_name;
if(a.openfda.generic\u name==b.openfda.generic\u name)
返回0;
if(a.openfda.generic\u nameb.openfda.generic\u name)
返回1;
});
drug
是一个数组,因此只需在循环之前对其进行排序。如何按“generic_name”对其进行排序?我试过使用drug[“openfda”].generic\u name.sort()
。它不起作用。我试着查看控制台,它说:无法读取未定义的
@Andreasdrug.sort()的属性“generic\u name”。sort()正在工作,但我不知道它使用什么来进行排序。我需要按“通用名称”排序@Andreas
//Sort JSON function
drug.sort(function(a,b){
//return a.openfda.generic_name - b.openfda.generic_name;
if(a.openfda.generic_name == b.openfda.generic_name)
return 0;
if(a.openfda.generic_name < b.openfda.generic_name)
return -1;
if(a.openfda.generic_name > b.openfda.generic_name)
return 1;
});