Javascript: push an array to an array with a for loop

Please explain this to me. I am trying to create an array of arrays with a for loop. When this did not work, I tried to simplify the code to understand what Javascript is doing, but simple code does not make sense either.

function test(){ var sub_array = []; var super_array =[]; for (var i=1;i<=3;i++){ sub_array.push(i); super_array.push(sub_array); } alert(super_array); } 

I expect to see [1; 1,2; 1,2,3]. Instead, I get [1,2,3; 1,2,3; 1,2,3]. I get the same phenomenon if I loop 0-2 and assign by index.

+6
source share
6 answers

You always push the link to the same array into your super array.

To solve this problem, you can use slice () to clone the submatrix before pressing it:

 function test() { var sub_array = []; var super_array = []; for (var i = 1; i <= 3; i++) { sub_array.push(i); super_array.push(sub_array.slice(0)); } alert(super_array); } 

EDIT:. As Dan D. points out, you can also call concat () with no arguments instead of slice(0) . This is faster in accordance with this article (I did not evaluate it myself):

 for (var i = 1; i <= 3; i++) { sub_array.push(i); super_array.push(sub_array.concat()); } 
+13
source

When you press "sub_array", you are not clicking on a copy of it. You get the same array three times in "super_array". (I have to say that you click the link to the same array three times.)

You can do it:

  // ... super_array.push(sub_array.slice(0)); 

to make a copy.

+6
source

well. You must understand that Array, Objects, Functions, etc. They are links in javascript (only Numbers (Int, Floats, etc.) And strings are transmitted "by value", which means that the value is copied / duplicated)! if you have var a=[]; , und say var b=a and add b.push("bla") and then warning a, will show you the entry "bla", even if you added it to b. In other words; a and b is javascript, like a note on mom’s frig, saying "the sand is on your left." And then you know what to take the left, and not some random sandwich from the refrigerator. She could also write another note (variable b) on the door of your house so that you know where to go and look for a sandwich if you are in a hurry. If she stuck a sandwich on the door ... well, that would be so. And JS also thinks about it :)

therefore, the solution to your problem is like vapor;

 function test(){ var super_array =[]; for (var i=1;i<=3;i++){ var subarray=[]; for (var u=1;u<=4-i;u++){ sub_array.push(u); super_array.push(subarray); } } alert(super_array); } 

by overriding the subarray, you create a new link. So the variable b (the second note on the door of the house) now points in the direction of another sandwich - perhaps a sandwich with dad.

Hope I could help you figure this out.

+2
source

Note that you are pushing the same array on super_array for each iteration in the for-loop. Instead, try the following:

 function test(){ var sub_array = []; var super_array =[]; for (var i=1;i<=3;i++){ sub_array = sub_array.slice(0,sub_array.length); sub_array.push(i); super_array.push(sub_array); } alert(super_array); } 
+1
source

This is the same sub_array that you add to super_array. So why should it be otherwise.

You do not create a new array or insert into super_array.

0
source

sub_array is stored as a reference in super_array , which means that when changing sub_array change is reflected inside super_array

0
source

Source: https://habr.com/ru/post/906878/


All Articles