为什么在不使用"if"函数的情况下无法访问数组项



我一直得到错误

"Cannot read properties of undefined (reading '2') at script.js:18:47" 

除非我把它放在if函数中。

这是阵列

['title','title',new Date("2022-03-26T10:00:00-06:00"),new Date("2022-04-02T10:00:00-06:00")],
['title','title',new Date("2022-04-02T10:00:00-06:00"),new Date("2022-04-09T10:00:00-06:00")],
['title', 'title', new Date("2022-04-09T10:00:00-06:00"), new Date("2022-04-16T10:00:00-06:00")],
['title', 'title', new Date("2022-04-16T10:00:00-06:00"), new Date("2022-04-23T10:00:00-06:00")],
['title', 'title', new Date("2022-04-23T10:00:00-06:00"), new Date("2022-04-30T10:00:00-06:00")],
['title', 'title', new Date("2022-04-30T10:00:00-06:00"), new Date("2022-05-07T10:00:00-06:00")],
['title', 'title', new Date("2022-05-07T10:00:00-06:00"), new Date("2022-05-14T10:00:00-06:00")],
['title', 'title', new Date("2022-05-14T10:00:00-06:00"), new Date("2022-05-21T10:00:00-06:00")],
['title', 'title', new Date("2022-05-21T10:00:00-06:00"), new Date("2022-05-28T10:00:00-06:00")]
];

不起作用:

var today = Date.now();
var weeksPassed = Math.floor((today - Date.parse(nextRelease))/(24*3600*1000*7));
var latestRelease = episodeList[weeksPassed][2];
var nextRelease = episodeList[weeksPassed][3];
document.getElementById("previousEpisode").innerHTML = episodeList[weeksPassed][0];
document.getElementById("nextEpisode").innerHTML = episodeList[weeksPassed][1]

作品:

var nextRelease = new Date("2022-03-26T10:00:00-06:00");
var today = Date.now();
if (today > nextRelease) {
var weeksPassed = Math.floor((today - Date.parse(nextRelease))/(24*3600*1000*7));
latestRelease = episodeList[weeksPassed][2];
nextRelease = episodeList[weeksPassed][3];
document.getElementById("previousEpisode").innerHTML = episodeList[weeksPassed][0];
document.getElementById("nextEpisode").innerHTML = episodeList[weeksPassed][1]
};

我不太擅长JavaScript,但我认为你在使用下一个版本时没有正确初始化它,所以从级联的行中会抛出一个异常

相关内容

  • 没有找到相关文章

最新更新