javascript for循环和数组推送

时间:2021-11-03 21:27:09

I'm trying to create an array of date objects starting from a certain date up till today.

我正在尝试从特定日期开始创建一个日期对象数组,直到今天。

Here's the code I have:

这是我的代码:

var beginning = new Date("04,06,2013");

var dates = [];
var today = new Date();

while (beginning < today){
    var x = beginning; 
    console.log(x);
    dates.push(x);
    beginning.setDate(beginning.getDate()+1)
}

for (var i in dates) {
  console.log(dates[i]);
}

In the while loop I see the correct dates incrementing but when I print out the dates in the array at the last for loop I see all the dates that are pushed being today's date.

在while循环中,我看到正确的日期递增但是当我在最后一个for循环中打印出数组中的日期时,我看到所有被推送的日期都是今天的日期。

Any ideas?

有任何想法吗?

2 个解决方案

#1


1  

What your code does is push a whole bunch of references to the exact same Date object. So, you have an array full of all the same Date object and each time you change that object, all elements in the array just point to the same object so they will all appear to change.

你的代码所做的是将一大堆引用推送到完全相同的Date对象。因此,您有一个完整的所有相同Date对象的数组,每次更改该对象时,数组中的所有元素都指向同一个对象,因此它们都会显示更改。

When you push an object into an array or assign an object to a variable, it does not make a copy, it pushes a reference to it (think of it like a pointer in other languages). To push different date objects for each iteration of the loop, you'd have to create a new date object each time through the loop and push that.

当您将一个对象推入一个数组或将一个对象分配给一个变量时,它不会复制它,它会推送一个对它的引用(把它想象成其他语言中的指针)。要为循环的每次迭代推送不同的日期对象,您必须每次通过循环创建一个新的日期对象并推送它。

In javascript, assigning an object or an array to any variable (which includes pushing it into an array) only assigns a reference to that object or array, not a copy. This is a common issue that bits most people coming up to speed on javascript.

在javascript中,将对象或数组分配给任何变量(包括将其推入数组)只分配对该对象或数组的引用,而不是副本。这是一个常见的问题,大多数人都会加速javascript。

You can make a new date object each time through the loop like this:

您可以每次通过循环创建一个新的日期对象,如下所示:

var beginning = new Date("04,06,2013");

var dates = [];
var today = new Date(), x;

while (beginning < today){
    x = new Date(beginning.getTime()); 
    console.log(x);
    dates.push(x);
    beginning.setDate(beginning.getDate()+1)
}

#2


0  

You're only working with one single Date instance throughout all that code.

您只在整个代码中使用一个Date实例。

To create a copy of a Date, do this:

要创建Date的副本,请执行以下操作:

x = new Date(beginning.getTime());

Then call the .setDate() method to move it forward.

然后调用.setDate()方法将其向前移动。

The setters on JavaScript Date instances change the object. They don't create a new one.

JavaScript Date实例上的setter更改了对象。他们没有创造新的。

#1


1  

What your code does is push a whole bunch of references to the exact same Date object. So, you have an array full of all the same Date object and each time you change that object, all elements in the array just point to the same object so they will all appear to change.

你的代码所做的是将一大堆引用推送到完全相同的Date对象。因此,您有一个完整的所有相同Date对象的数组,每次更改该对象时,数组中的所有元素都指向同一个对象,因此它们都会显示更改。

When you push an object into an array or assign an object to a variable, it does not make a copy, it pushes a reference to it (think of it like a pointer in other languages). To push different date objects for each iteration of the loop, you'd have to create a new date object each time through the loop and push that.

当您将一个对象推入一个数组或将一个对象分配给一个变量时,它不会复制它,它会推送一个对它的引用(把它想象成其他语言中的指针)。要为循环的每次迭代推送不同的日期对象,您必须每次通过循环创建一个新的日期对象并推送它。

In javascript, assigning an object or an array to any variable (which includes pushing it into an array) only assigns a reference to that object or array, not a copy. This is a common issue that bits most people coming up to speed on javascript.

在javascript中,将对象或数组分配给任何变量(包括将其推入数组)只分配对该对象或数组的引用,而不是副本。这是一个常见的问题,大多数人都会加速javascript。

You can make a new date object each time through the loop like this:

您可以每次通过循环创建一个新的日期对象,如下所示:

var beginning = new Date("04,06,2013");

var dates = [];
var today = new Date(), x;

while (beginning < today){
    x = new Date(beginning.getTime()); 
    console.log(x);
    dates.push(x);
    beginning.setDate(beginning.getDate()+1)
}

#2


0  

You're only working with one single Date instance throughout all that code.

您只在整个代码中使用一个Date实例。

To create a copy of a Date, do this:

要创建Date的副本,请执行以下操作:

x = new Date(beginning.getTime());

Then call the .setDate() method to move it forward.

然后调用.setDate()方法将其向前移动。

The setters on JavaScript Date instances change the object. They don't create a new one.

JavaScript Date实例上的setter更改了对象。他们没有创造新的。