提问人:Anton 提问时间:4/23/2023 更新时间:4/23/2023 访问量:28
在 Typescript/Javascript [duplicate] 中将 Date[] 添加到 Date[][] 中的奇怪行为
Odd behaviour when adding Date[] into Date[][] in Typescript/Javascript [duplicate]
问:
因此,我需要计算一组时间间隔,如下所示: 我决定为此使用 Date 类型,并编写了这段代码来测试:[8:00 - 8:10]
let todaysDate = new Date();
let startTime: Date = new Date(todaysDate.setHours(5, 0, 0));
let endTime: Date = new Date(todaysDate.setHours(6, 0, 0));
let start : number = startTime.getTime();
let interval: number = 600000;
let firstTime = new Date(startTime.getTime());
let secondTime = new Date(startTime.getTime() + interval);
let timeIntervalsArray: Date[][] = [];
while (secondTime.getTime() < endTime.getTime()) {
firstTime.setTime(start);
secondTime.setTime(firstTime.getTime() + interval);
const times: Date[] = [firstTime, secondTime];
timeIntervalsArray.push(times);
start += interval;
}
console.log(timeIntervalsArray);
但是在记录 timeIntervalsArray 变量时,我得到数组中的所有变量都是相同的:
[LOG]:
[[Date: "2023-04-22T05:50:00.722Z", Date: "2023-04-22T06:00:00.722Z"],
[Date: "2023-04-22T05:50:00.722Z", Date: "2023-04-22T06:00:00.722Z"],
[Date: "2023-04-22T05:50:00.722Z", Date: "2023-04-22T06:00:00.722Z"],
[Date: "2023-04-22T05:50:00.722Z", Date: "2023-04-22T06:00:00.722Z"],
[Date: "2023-04-22T05:50:00.722Z", Date: "2023-04-22T06:00:00.722Z"],
[Date: "2023-04-22T05:50:00.722Z", Date: "2023-04-22T06:00:00.722Z"]]
我设法通过更改以下行来修复此行为:const times: Date[] = [firstTime, secondTime];
const times: Date[] = [new Date(firstTime), new Date(secondTime)];
但我不明白这是如何工作的,就好像我是这样记录的一样:
while (secondTime.getTime() < endTime.getTime()) {
firstTime.setTime(start);
secondTime.setTime(firstTime.getTime() + interval);
//Logging firstTime
console.log(firstTime);
console.log('As new date', new Date(firstTime));
//
const times: Date[] = [firstTime, secondTime];
timeIntervalsArray.push(times);
start += interval;
}
我得到相同的 Data 对象:
[LOG]: Date: "2023-04-22T05:00:00.718Z"
[LOG]: "As new date", Date: "2023-04-22T05:00:00.718Z"
更奇怪的是 - 在记录 timeIntervalsArray 本身时:
while (secondTime.getTime() < endTime.getTime()) {
firstTime.setTime(start);
secondTime.setTime(firstTime.getTime() + interval);
//Logging timeInterval
console.log(timeIntervalsArray);
//
const times: Date[] = [firstTime, secondTime];
timeIntervalsArray.push(times);
start += interval;
}
Fist iteration : [LOG]: [] -> array is empty
Second iteration :
[LOG]: [[Date: "2023-04-22T05:10:00.241Z", Date: "2023-04-22T05:20:00.241Z"]]
-> array has one element - [Date: "2023-04-22T05:10:00.241Z", Date: "2023-04-22T05:20:00.241Z"]
Third iteration :
[LOG]: [[Date: "2023-04-22T05:20:00.241Z", Date: "2023-04-22T05:30:00.241Z"],
[Date: "2023-04-22T05:20:00.241Z", Date: "2023-04-22T05:30:00.241Z"]]
-> the fist element has been changed to
[Date: "2023-04-22T05:20:00.241Z", Date: "2023-04-22T05:30:00.241Z"]
but it was
[Date: "2023-04-22T05:10:00.241Z", Date: "2023-04-22T05:20:00.241Z"]
有人可以解释这种行为,因为我没有想法......
答: 暂无答案
下一个:为什么此数组不保存扫描的数字?
评论
new Date
setTime
const times: Date[] = [new Date(firstTime), new Date(secondTime)];