JavaScript对象数组在页面加载期间返回长度为零,但在控制台中不返回
JavaScript array of objects returns length of zero during page load, but not in console?
我正在处理以下页面:
http://www.waisman.wisc.edu/
问题代码(在线,可在页面第848行找到):
<script>
var listType = "Basic",
theList92 = document.querySelector('#block-92 > ol'),
finalEventsList92 = "",
futureEvents92 = [],
pastEvents92 = [],
nonWiley92 = [],
events92 = [{
title: "Golf Benefit 2015",
date: new Date("2015, 7, 20"),
time: "10:30 am",
series: "Friends of the Waisman Center",
speaker: "",
thumbnail: "/images/Golf/GolfPhoto75x100.jpg",
location: "Bishops Bay Country Club", url: "/events2015-GolfBenefit.htm"
},{
title: "David Stokes",
date: new Date("2015, 5, 10"),
time: "1 pm",
series: "Children's Theatre",
speaker: "David Stokes",
thumbnail: "/images/CT/StokesDavidPuppets75x100.jpg",
location: "Friends of the Waisman Center Auditorium", url: "/events-ctS2015-Stokes.htm"
},{
title: ""The Genetics of Cerebellar Development and Function"",
date: new Date("2015, 4, 24"),
time: "Noon",
series: "John D. Wiley Seminar Series",
speaker: "Dan Goldowitz, PhD",
thumbnail: "/images/Seminars/GoldowitzDan75x100.jpg",
location: "John D. Wiley Conference Center", url: "/seminars-2015-Apr24-Goldowitz.htm"
},{
title: ""The Infant Brain Imaging Study (IBIS): Insights into the Early Development of Autism"",
date: new Date("2015, 4, 17"),
time: "Noon",
series: "John D. Wiley Seminar Series",
speaker: "Joseph Piven, MD",
thumbnail: "/images/Seminars/PivenJoe75x100.jpg",
location: "John D. Wiley Conference Center", url: "/seminars-2015-Apr17-Piven.htm"
},{
title: "Snow White",
date: new Date("2015, 4, 12"),
time: "1 pm",
series: "Children's Theatre",
speaker: "PlayTime Productions",
thumbnail: "/images/CT/PlaytimeBanner75x100.jpg",
location: "Friends of the Waisman Center Auditorium", url: "/events-ctS2015-SnowWhite.htm"
},{
title: ""Synaptic and Circuitry Mechanisms of Psychiatric Disorders"",
date: new Date("2015, 4, 10"),
time: "Noon",
series: "John D. Wiley Seminar Series",
speaker: "Guoping Feng, PhD",
thumbnail: "/images/Seminars/FengGuoping75x100.jpg",
location: "John D. Wiley Conference Center", url: "/seminars-2015-Apr10-Feng.htm"
},{}];
events92.pop();
if(events92.length === 0){
finalEventsList92 = "<li>No matching events listed at this time.</li>";
} else {
if("Yes" === "Yes"){
events92.forEach(function(wcEvent, index, array){
if(wcEvent.series !== "John D. Wiley Seminar Series"){
nonWiley92.push(wcEvent);
}
});
events92 = nonWiley92.reverse();
}
if("Future" === "Future"){
events92.forEach(function(wcEvent, index, array){
if(wcEvent.date >= Date.now()){
futureEvents92.push(wcEvent);
}
});
futureEvents92.reverse();
if(futureEvents92.length === 0){
finalEventsList92 = "<li>No matching events listed at this time.</li>";
} else {
futureEvents92.reverse();
for(var i = 0; i < 6; i++){
if(futureEvents92[i]){
if(listType === "Basic"){
finalEventsList92 += buildBasicListItem(futureEvents92[i]);
} else {
finalEventsList92 += buildListItemWithImage(futureEvents92[i]);
}
}
}
}
}
if("Future" === "Past"){
events92.forEach(function(wcEvent, index, array){
if(wcEvent.date < Date.now()){
pastEvents92.push(wcEvent);
}
});
if(pastEvents92.length === 0){
finalEventsList92 = "<li>No matching events listed at this time.</li>";
} else {
pastEvents92.reverse();
for(var i = 0; i < 6; i++){
if(pastEvents92[i]){
if(listType === "Basic"){
finalEventsList92 += buildBasicListItem(pastEvents92[i]);
} else {
finalEventsList92 += buildListItemWithImage(pastEvents92[i]);
}
}
}
}
}
if("Future" === "All"){
for(var i = 0; i < 6; i++){
if(events92[i]){
if(listType === "Basic"){
finalEventsList92 += buildBasicListItem(events92[i]);
} else {
finalEventsList92 += buildListItemWithImage(events92[i]);
}
}
}
}
}
theList92.innerHTML = finalEventsList92;
</script>
在最初评估events92.length === 0
的所有浏览器中,在控制台中,运行相同的命令实际上会给出正确的长度(在本例中为66)。
此外,我知道有一些奇怪的比较(比如"Future" === "Future"
,但列表是由CMS生成的,这就是我们允许用户调整列表显示的方式,因为CMS不像WordPress那样是动态的。
感谢您的帮助。
好吧,我想明白了。
我使用了以下内容:
IE和Safari无法正确解析的new Date("2014, 01, 01")
。我刚刚删除了引号,一切似乎都正常工作。
奇怪的是,它不会在控制台中抛出错误,但当你查看对象时,它会说"无效日期"。
我也不知道为什么这会影响array.length属性,但它确实会影响(至少在IE/Safari中)。
因此,对于那些可能使用相同格式的人,只需删除引号:
new Date(2014, 01, 01)
相关文章:
- 控制台返回var不是't定义,但它是
- Meteor js-控制台显示'未定义'即使返回结果
- 函数将信息返回到控制台日志,但实际上并没有执行应有的操作
- 将Function-Nothing日志记录返回到控制台
- 控制台返回[Object-Object]而不是对象成员
- 为什么此 IF 语句不返回控制台消息
- 为什么返回的 Promise 在登录到控制台时没有方法
- 在 Javascript 函数中,控制台.log和返回有什么区别
- Javascript在IE7,8,9中返回控制台错误,但在所有其他浏览器中都能完美运行
- 从控制台获取值.log并在输入字段中显示/打印/返回
- 控制台.log返回一个额外的未定义
- javascript 的 document.getElementById 返回 null,但从控制台工作
- 我无法让它作为 JavaScript 函数工作(我在控制台时得到我想要的返回值.log但我不能将其作为函数调用)
- 为什么当我将变量记录到控制台时,我得到了输出,但是当我返回它时,我收到“未定义”
- 控制台日志记录“;这个“;返回“;空”;
- OOOjavascript:object self-reference返回undefined,但控制台显示它存在
- javascript输出返回控制台
- 查询返回控制台日志中的项目,但是html显示Uncaught TypeError: Cannot call method
- 使用文本输入框复制代码以搜索解析将返回控制台错误
- nodejs猫鼬没有't保存条目后返回控制台