我在我的项目中使用维基百科API,并希望将每个项目(维基百科中的文章)与有关文章(也来自维基百科)的相关信息相关联。例如,孔子与孔子有关。生物学,孔子学院有关大学的相关信息等等。当我循环通过它带来一团糟。例如
从图中可以看出,标题(孔子)与红色下面的文字无关。我知道for循环的问题,我尝试了不同的方法,但失败了。请帮帮我!
var wikirequest = function() {
$.ajax({
url:url,
dataType: 'jsonp',
success: function(wikiData) {
// Fetch the biographical information
var bioName = wikiData[2][0];
// Check if instead of bio there is a phrase "The article may refer to...." if so then change indices
if (bioName.indexOf('may refer to') >= 0) {
bioName = wikiData[2][1];
} else {
var bioName = wikiData[2][0];
}
var wikiArcticles = wikiData[1];
var wikiArticlesShortInfo = wikiData[2]
console.log("This is short info "+ wikiArticlesShortInfo);
//domCache.$wikiArticlesList.empty();
domCache.$wikiArticlesList.html('');
for (var i=0; i < wikiArcticles.length; i++) {
for ( var j=0; j <wikiArticlesShortInfo.length; j++ ) {
console.log("Each short story " + wikiArticlesShortInfo[j]);
console.log("Each article " + wikiArcticles[i]);
domCache.$wikiArticlesList.append('<li class="articleItem">'+ wikiArcticles[i]+ '<br>'+ '<span class="shortInfo">'+ wikiArticlesShortInfo[j]+'</span>'+ '</li>');
}
}
// Short biography
console.log(bioName);
console.log(wikiArcticles);
console.log(url);
domCache.$bioDiv.text(bioName);
} // end of success
});
}// wikirequest
wikirequest();
答案 0 :(得分:0)
for (var j=0; j < wikiData.length; j++) {
var articleAuthor = wikiData[1][j];
var articleInfo = wikiData[2][j];
var linkAuthor = wikiData[3][j];
domCache.$wikiArticlesList.append('<li class="articleItem">'+ '<span>' + '<a href =' + linkAuthor + ' target="_blank" >' + articleAuthor + '</a>' + '</span>' +'<span class="shortInfo">' + articleInfo +'</span>' + '</li>');
// Check if some articles are undefined if so hide them
if (articleAuthor === "undefined") {
$('.articleItem').html('');
}
//console.log(j + " " + articleAuthor);
//console.log(j + " " + articleInfo);
//console.log(j + " " + linkAuthor);
}