简体   繁体   English

在Javascript中将数组推送到多维数组

[英]Push an array to a multidimensional array in Javascript

I'm trying to take an array, inArray , that changes with each iteration of a for() loop and push it to a multidimensional array, medArray .我正在尝试获取一个数组inArray ,该数组随着for()循环的每次迭代而变化,并将其推送到多维数组medArray There are two different methods below that are slightly different and one achieves the goal whereas the second outputs medArray such that is has three arrays within it and they are all equal to the last iteration of inArray .下面有两种不同的方法略有不同,一种实现了目标,而第二个输出medArray使得其中包含三个数组,它们都等于inArray的最后一次迭代。

I'm confused as to why this works:我很困惑为什么这有效:

var inArray = [1, 2, 3];

var medArray = [];



for(i = 0; i < 3; i++){
    inArray[i] += 1;
//The difference is this line below
    var test = [inArray[0], inArray[1], inArray[2]];
    medArray.push(test)
    console.log(medArray);
 }

But this doesn't work:但这不起作用:

var inArray = [1, 2, 3];

var medArray = [];

 for(i = 0; i < 3; i++){
    inArray[i] += 1;
//The difference is this line below
    var test = inArray;
    medArray.push(test)
    console.log(medArray);
 }

When I individually assign the values of test to be the individual value of inArray it works but when I assign the two arrays to be equal, it doesn't work.当我单独将test的值分配为inArray的单独值时,它可以工作,但是当我将两个数组分配为相等时,它不起作用。 What is happening?怎么了?

the second outputs medArray such that is has three arrays within it and they are all equal to the last iteration of inArray .第二个输出medArray使得其中包含三个数组,它们都等于inArray的最后一次迭代。

Actually, the second code block pushes the same array to medArray three times.实际上,第二个代码块将同一个数组推送到medArray三次。 When you say temp = inArray that doesn't create a copy of the array, it creates another reference to the same array.当您说temp = inArray不会创建数组的副本时,它会创建对同一数组的另一个引用。 (So if, after the loop, you were to say medArray[0][0] = 10 you'd find that medArray[1][0] and medArray[2][0] now both equal 10 too, because medArray[0] , medArray[1] and medArray[2] all reference the same array.) (因此,如果在循环之后,您要说medArray[0][0] = 10您会发现medArray[1][0]medArray[2][0]现在也都等于10 ,因为medArray[0]medArray[1]medArray[2]都引用同一个数组。)

If you want to make a copy of the inArray array then use the array .slice() method :如果要复制inArray数组,请使用数组.slice()方法

var temp = inArray.slice();

Then temp would be a new array that has the same elements as inArray .那么temp将是一个与inArray具有相同元素的新数组。 Which is what you did in your first code block with test = [inArray[0], inArray[1], inArray[2]];这就是你在第一个代码块中用test = [inArray[0], inArray[1], inArray[2]]; . .

Or you can omit the temp variable and just say:或者你可以省略temp变量而只说:

medArray.push(inArray.slice());

( .slice() can also be used to copy just a portion of an array if you call it with arguments, but called with no arguments it copies the whole array.) .slice()也可用于仅复制数组的一部分,如果您带参数调用它,但不带参数调用它会复制整个数组。)

In context:在上下文中:

 var inArray = [1, 2, 3]; var medArray = []; for(var i = 0; i < 3; i++){ inArray[i] += 1; medArray.push(inArray.slice()); } console.log(medArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM