gpt4 book ai didi

javascript - JS/JQ : Schrodinger's array? 一个数组怎么可以同时为空和不为空?

转载 作者:行者123 更新时间:2023-11-30 16:22:36 27 4
gpt4 key购买 nike

我有一个返回对象数组的 javascript 函数。该数组如下所示:array

我想使用 $.each() 遍历数组并对对象的属性做一些事情。但是, $.each() 根本不会触发。例如,这个:

myArray = myFunc();
$.each(myArray, function(index, object){
console.log('testing testing 1 2 3');
}

什么都不做。没有 console.logs。

但是,如果我这样做:

myArray = myFunc();
console.dir(myArray);
$.each(, function(index, object){
console.log('testing testing 1 2 3');
}

在 Firefox 中,它会产生如上所示的输出 - 我可以很好地看到数组中的所有对象。

然而,console.log() 显示简单

Array [   ]

数组怎么可能同时为空,并且具有 console.dir() 可以显示的内容?

FWIW,这个丑陋的困惑是函数(部分由 PHP 生成)最终的样子:

            instance = false;
function checkMessages() {
var messageArray = [];
$('#newMessages').html('0');
if (!instance) {
instance = true;
myMessages = 0;
myRead = 0;
delete data;
$.ajax({
type : 'POST',
url : 'Chat/process.php',
data : {
'function' : 'getState',
'filename' : 'data/chat_2_1.txt',
},
dataType: 'json',
success: function(data){
myMessages = data.state;
messageArray[2] = {
'filename' : 'chat_2_1.txt',
'divname' : 'msg_2',
'length' : data.state,
};
},
}).done(function(){
delete data;
$.ajax({
type : 'POST',
url : 'Chat/process.php',
data : {
'function' : 'getReadByMe',
'filename' : 'log/chat_2_1_2.txt',
},
dataType: 'json',
success: function(data){
messageArray[2].read = data.read;
},
});


});
myMessages = 0;
myRead = 0;
delete data;
$.ajax({
type : 'POST',
url : 'Chat/process.php',
data : {
'function' : 'getState',
'filename' : 'data/chat_3_1.txt',
},
dataType: 'json',
success: function(data){
myMessages = data.state;
messageArray[3] = {
'filename' : 'chat_3_1.txt',
'divname' : 'msg_3',
'length' : data.state,
};
},
}).done(function(){
delete data;
$.ajax({
type : 'POST',
url : 'Chat/process.php',
data : {
'function' : 'getReadByMe',
'filename' : 'log/chat_3_1_2.txt',
},
dataType: 'json',
success: function(data){
messageArray[3].read = data.read;
},
});
});
instance = false;
return messageArray;
} else {
// setTimeout(checkMessages({}), 500);
}

}



$(document).ready(function(){
console.log('foo');
myMessageArray = checkMessages([]);
console.log(myMessageArray);
$.each(myMessageArray, function(i, obj){
console.log('compare?');
// compareRead(Number(obj['length']), Number(obj['read']), obj['divname']);
});
// setInterval(checkMessages(), 500);
});

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com