JavaScript对象数组在页面加载期间返回长度为零,但在控制台中不返回

JavaScript array of objects returns length of zero during page load, but not in console?

本文关键字:返回 控制台 对象 加载 JavaScript 数组      更新时间:2023-09-26

我正在处理以下页面:

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: "&quot;The Genetics of Cerebellar Development and Function&quot;",
                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: "&quot;The Infant Brain Imaging Study (IBIS): Insights into the Early Development of Autism&quot;",
                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: "&quot;Synaptic and Circuitry Mechanisms of Psychiatric Disorders&quot;",
                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)