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.
source share