【问题标题】:JavaScript/AppsScript - Creating an array using a while loop overwrites the first element in arrayJavaScript/AppsScript - 使用 while 循环创建数组会覆盖数组中的第一个元素
【发布时间】:2021-03-23 17:50:33
【问题描述】:

我正在尝试在给定的时间范围内创建一个包含所有日期的数组。出于测试目的,我使用的是 2 月 9 日 - 2 月 13 日,因此所需的数组应该是 [Feb 9, Feb 10, Feb 11, Feb 12, Feb 13]。感谢一篇旧文章,我学会了每次都使用 New Date() 创建新的 Date 对象,这样数组就不会像 [Feb 13, Feb 13, Feb 13, Feb 13, Feb 13] 那样结束。但是,它仍然覆盖了我推送到数组的第一个元素;它正在输出 [2 月 10 日、2 月 11 日、2 月 12 日、2 月 13 日、2 月 13 日]。我在 while 循环中包含了一个打印语句,用于在每次推送后跟踪数组,这就是我知道第一个元素是被覆盖的元素的方式。我希望能深入了解为什么会发生这种情况,以及如何获得我想要的阵列?非常感谢任何帮助,谢谢!

这是我正在使用的代码:

  var Current_Date = new Date(Date_start) // It starts at Feb 9 
  Current_Date = new Date(Current_Date.setHours(Current_Date.getHours() - 24)); // Feb 8 
  var Billing_Dates = []

 
  while (Current_Date.valueOf() <= Date_end.valueOf()) {
    Current_Date = new Date(Current_Date.setHours(Current_Date.getHours() + 24)); 
    Billing_Dates.push(Current_Date); 
    Logger.log(Billing_Dates)
    
  }
}

简化输出(包括每次推送后数组的状态):

[Feb 09]
[Feb 10, Feb 10]
[Feb 10, Feb 11, Feb 11]
[Feb 10, Feb 11, Feb 12, Feb 12]
[Feb 10, Feb 11, Feb 12, Feb 13, Feb 13]

Image of the Exact Output (including the status of the array after every push)

【问题讨论】:

    标签: javascript arrays datetime google-apps-script


    【解决方案1】:

    当你这样做时

    Billing_Dates.push(Current_Date)
    

    您正在推送对日期的引用,而不是日期本身。因此,在下一个循环中,您更改日期并且引用它的任何内容也会更改。这就是为什么数组中的最后两个日期总是相同的原因。在这一行

    Current_Date = new Date(Current_Date.setHours(Current_Date.getHours() + 24)); 
    

    您通过执行 new Date() 创建一个新引用,这就是它只修改数组中最后两个元素的原因。

    要修复,请在存储到数组时创建一个新引用:

    Billing_Dates.push(new Date(Current_Date))
    

    【讨论】:

      【解决方案2】:

      不要复制日期的引用并就地修改它们,而是每次创建一个新的,例如:

      const numDays = 5
      const oneDayOffset = 24 * 3600 * 1000;
      const dates = [];
      const startDate = new Date();
      
      for (let i=0; i < numDays; i++){
        const newDate = new Date( startDate.getTime() + i * oneDayOffset );
        dates.push(newDate.toString())
      }
      
      console.log(dates)

      【讨论】:

        【解决方案3】:
        function makeDateArray(start = '2/9/2021',end = '2/13/2021') {
          let da = [];
          let sd = new Date(start);
          let ed = new Date(end);
          let n = 0;
          do {
            da.push(new Date(sd.getFullYear(), sd.getMonth(), sd.getDate() + n++));
          } while (new Date(da[da.length - 1]).valueOf() < ed.valueOf());
          let db=da.map((d)=>{return Utilities.formatDate(new Date(d),Session.getScriptTimeZone(),"MMM dd")});
          Logger.log(db);
        }
        
        Execution log
        12:28:30 PM Notice  Execution started
        12:28:30 PM Info    [Feb 09, Feb 10, Feb 11, Feb 12, Feb 13]
        12:28:30 PM Notice  Execution completed
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-09-15
          • 1970-01-01
          • 2021-03-20
          • 2021-04-17
          • 2016-09-18
          • 2018-07-02
          • 2011-05-03
          相关资源
          最近更新 更多