每次迭代期间的日期更新

时间:2019-04-07 08:52:52

标签: javascript node.js date

我想将对象数组(名为A)插入mongodb。数组中的每个对象都包含一个日期变量和其他对象。我想在数组A中插入2个(说)对象,并从今天开始连续两天直到第二天。我编写了以下while循环来做到这一点:

score_array = [];
date = new Date("2019-04-07");
num_days = 2;
name = "abc"
x = 0
i=1
while(i<=num_days){
    console.log("<--------ITERATION "+ i + " --------->")
    date.setDate(date.getDate() + 1);
    x = x +10;
    score_details = {score:x,status : 0};
    console.log(date)
    final_list = {date:date,score_details};
    console.log("printing final_list.......")
    console.log(final_list);
    score_array.push(final_list);
    console.log("printing score_array.......")
    console.log(score_array);
    i=i+1;
  }

当我使用某些打印功能运行此代码时,结果如图所示。

<--------ITERATION 1 --------->
2019-04-08T00:00:00.000Z
printing final_list.......
{ date: 2019-04-08T00:00:00.000Z,
  score_details: { score: 10, status: 0 } }
printing score_array.......
[ { date: 2019-04-08T00:00:00.000Z,
    score_details: { score: 10, status: 0 } } ]
<--------ITERATION 2 --------->
2019-04-09T00:00:00.000Z
printing final_list.......
{ date: 2019-04-09T00:00:00.000Z,
  score_details: { score: 20, status: 0 } }
printing score_array.......
[ { date: 2019-04-09T00:00:00.000Z,
    score_details: { score: 10, status: 0 } },
  { date: 2019-04-09T00:00:00.000Z,
    score_details: { score: 20, status: 0 } } ]

为什么对象0的日期在第二次迭代中得到更新。我想将每天的日期存储在数组中。我希望最终数组像这样

[ { date: 2019-04-08T00:00:00.000Z,
    score_details: { score: 10, status: 0 } },
  { date: 2019-04-09T00:00:00.000Z,
    score_details: { score: 20, status: 0 } } ]

0 个答案:

没有答案