の最後に評価された場合にconsole.logは空の配列を提供します。私はtemp1.keywords
の各要素の同義語を取得するためのAPIリクエストを作成することができるよjQueryの - このパターンで配列<code>temp1</code>有する符号
[{"group":"Test","keywords":["Hund","Katze"]}]
を次の関数で:
consultSynonyms(temp1)
function consultSynonyms(x) {
// create an empty array where all the responses are gping to be pushed
// format has to be: [{x: xvalue, y: yvalue, z:zvalue...}]. This array
// will be passed to data table plugin
var jsonSynonyms = [];
$.each(x, function(i, value) {
// get name of group (f.i. Test)
var superGroup = value.group;
$.each(x[i].keywords, function(i, value) {
// get term (f.i. Hund) and iterate all the terms
var term = value;
$.ajax({
type: "GET",
dataType: "jsonp",
url: "https://www.openthesaurus.de/synonyme/search?q=" + term + "&format=application/json&supersynsets=true",
success: function(data) {
$.each(data.synsets, function(key, value) {
// get category (f.i. "Biology") from response
var category = this.categories[0];
$.each(this.terms, function(key, value) {
// synonym is every term delievered by the API Call
// level is also delievered (f.i.: "formal", "informal"...)
var synonym = this.term;
var level = this.level;
jsonSynonyms.push({
group: superGroup,
keyword: term,
category: category,
term: synonym,
level: level
});
});
});
}
});
});
});
console.log(jsonSynonyms);
}
しかし、最後console.log
はdoesn't期待される出力が、簡単な[]
を提供します。私の知る限りcorcernedだとして
[{"group":"Unnamed group","keyword":"Hund","category":"Biologie","term":"Hund"},{"group":"Unnamed group","keyword":"Hund","category":"Biologie","term":"Vierbeiner"}...]
、終わりにconsole.log:私はfunction(data)
内にconsole.logを移動した場合、私が期待した出力を得るので、私は、Ajaxコードが正しいことを確信しています私の関数の最後に評価されるので、私は空の配列を取得している理由を理解していない、関数の途中でそれを評価する正しい出力を提供します。この動作のヒント?