为什么不使用 "if" 函数就无法访问数组项
Why can't I access the array item without using an "if" function
我一直收到错误消息
"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,但我认为你在使用下一个版本时没有正确初始化它,所以从那个
级联的行会抛出异常
我一直收到错误消息
"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,但我认为你在使用下一个版本时没有正确初始化它,所以从那个
级联的行会抛出异常