如果在代码末尾进行求值,jQuery-console.log将提供空数组
jQuery - console.log delivers empty array if evaluated at the end of the code
具有以下模式的数组temp1
:
[{"group":"Test","keywords":["Hund","Katze"]}]
我可以通过以下函数发出API请求,以获取temp1.keywords
的每个元素的同义词:
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
并没有提供预期的输出,而是一个简单的[]
。我确信ajax代码是正确的,因为如果我在function(data)
中移动console.log,我会得到预期的输出:
[{"group":"Unnamed group","keyword":"Hund","category":"Biologie","term":"Hund"},{"group":"Unnamed group","keyword":"Hund","category":"Biologie","term":"Vierbeiner"}...]
就我而言,函数末尾的console.log是在最后求值的,因此我不明白为什么我得到的是空白数组,而在函数中间求值可以提供正确的输出。这种行为有什么暗示吗?
您使用AJAX请求,该请求开始异步执行其中的代码。一旦您异步,就永远不会返回。
您可以看到,jsonSynonyms
直到AJAX调用返回后才会被填充。这可能是任何时间,可能是100-200ms。但是,在进行AJAX调用后,console.log(jsonSynonyms)
代码会立即执行。因此jsonSynonyms
不可能被填充。
您只能在AJAX调用返回后使用填充的jsonSynonyms
。这意味着您应该在实际的AJAX回调中使用它。
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); // look at me now!
}
});
});
});
}
相关文章:
- 如果在代码末尾进行求值,jQuery-console.log将提供空数组
- jQuery .text() 不出现在 html 中,而是出现在 console.log 中
- 为什么会失败: 'function log(){console.log.apply(null,arguments);}`
- Casperjs catch console.log and console.error
- 为什么 alert() 会触发而不是 console.log() 对于这个 jQuery 在 Greasemonkey
- jQuery-Do alert 命令在 javascript 或 jquery 中的 console.log() 之前编
- IE 9中的Javascript更改bgColor兼容问题?(没有JQuery)(没有console.log)
- Javascript/jQuery:为什么event.target使用<a>标记console.log,al
- 在console.log中调用jQuery实例时,jQuery实例如何显示为数组
- 如何最安全地使用console.log和console.warn
- 我如何创建一个函数,每次jQuery绑定到单击事件时,它都会详细记录console.log
- 为什么这个JQuery只工作,如果我console.log一个坏变量
- JQUERY,PHP POST -在console.log中显示00
- 如何在jquery中获取console.log数据
- jQuery .on()问题,console.log()工作,但元素交互不't
- JQuery问题打印对象到console.log
- jQuery console.log上任何元素的触发器
- jQuery console.log();不适用于Firefox
- ' console.log([]) '和' console.log([]. tostring()) '提供不同的输出
- console.log和console.dir之间有什么不同