在 Typescript/Javascript [duplicate] 中将 Date[] 添加到 Date[][] 中的奇怪行为

Odd behaviour when adding Date[] into Date[][] in Typescript/Javascript [duplicate]

提问人:Anton 提问时间:4/23/2023 更新时间:4/23/2023 访问量:28

问:

因此,我需要计算一组时间间隔,如下所示: 我决定为此使用 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"]

有人可以解释这种行为,因为我没有想法......

JavaScript 数组 TypeScript 日期 while 循环

评论

2赞 Bergi 4/23/2023
"我知道数组中的所有变量都是相同的“——是的,准确地说,它们都是同一个对象。代码仅创建两个对象。 更改现有对象,并且没有复制它们的操作。您已经发现了修复:确实创建了新对象。new DatesetTimeconst times: Date[] = [new Date(firstTime), new Date(secondTime)];
0赞 Anton 4/23/2023
嗯,是的,我更改了现有对象,但随后将其推送到数组。数组中的对象添加到数组后如何更改?
0赞 GarfieldKlon 4/23/2023
因为他们持有对它的引用。firstTime 和 secondTime 仍在数组中引用这些对象。您将同一对象放入数组中 6 次,并且更新此对象 6 次。它显示你最后的 setTime。您可以在循环中记录您的数组,您将看到它。-> console.log(JSON.stringify(timeIntervalsArray))
0赞 Bergi 4/23/2023
@Anton 向数组添加某些内容不会复制它。它仍然是对同一对象的引用,即您稍后要更改的对象

答: 暂无答案