0

EDIT: the problem I came across was that I did not know that js arrays are treated as objects and any as so are referenced not copied. If you were interested in simply reversing a js array you can use...reverse();

I thought I would be able to simply do the following in javascript

var originalArray = [1,2,3,5,8,13];
var originalArrayCOPY = originalArray;
console.log ("new copy = "+originalArrayCOPY);

for(var zyx = 0; zyx <6; zyx++){
    var xyz = 5-zyx;
    originalArray[zyx] = originalArrayCOPY[xyz];
}

console.log("original now "+originalArray);
console.log("copy ="+originalArrayCOPY);

But my results are so strange that I feel I must not understand javascript at all!!

new copy = 1,2,3,5,8,13

original now 13,8,5,5,8,13,

copy =13,8,5,5,8,13,

I can-t see why this wouldn-t work and I honestly am not sure why the COPY of the original array is changed at all.

If I could at least name the problem I could ask google, also if you tell me the name of the problem I will rename by SO question.

EnglishAdam
  • 1,380
  • 1
  • 19
  • 42
  • don't use such variables names...it confuses most of the people. – amit_183 Nov 17 '14 at 09:22
  • 3
    Cannot disagree with your comment about your understanding of Javascript. You can call it a copy, but it's not, it's the exact same array. If you want a real copy, try `originalArray.slice()`. –  Nov 17 '14 at 09:22
  • 1
    Obviously the two arrays "contain" the same data. So the assignment performed a shallow copy when you wanted a deep copy. http://en.wikipedia.org/wiki/Object_copy –  Nov 17 '14 at 09:24
  • @torazaburo, i can see that-s what happened, in a programming language what would that be called where you can or can-t straight copy an array? is it the same for variables? – EnglishAdam Nov 17 '14 at 09:25
  • 1
    @YvesDaoust It's not even a shallow copy. It's just two references to the same object. – Robby Cornelissen Nov 17 '14 at 09:25
  • @RobbyCornelissen: yep, one reference was copied to the other. –  Nov 17 '14 at 09:26
  • But for example PHP and js differ here, how can I check which way a language works with var = var? – EnglishAdam Nov 17 '14 at 09:28
  • Your question seems to be similar to either [Copying array by value in JS](http://stackoverflow.com/questions/7486085/copying-array-by-value-in-javascript) or [Most efficient way to reverse an array in JS](http://stackoverflow.com/questions/5276953/what-is-the-most-efficient-way-to-reverse-an-array-in-javascript) – Paul Nov 17 '14 at 09:39

1 Answers1

2

use this:

var originalArray = [1,2,3,5,8,13];
var originalArrayCOPY = originalArray.slice();
console.log ("new copy = "+originalArrayCOPY);
for(var zyx = 0; zyx <6; zyx++){
    var xyz = 5-zyx;
    originalArray[zyx] = originalArrayCOPY[xyz];
}

console.log("original now "+originalArray);
console.log("copy ="+originalArrayCOPY);

'=' copy references and slice() will produce a new copy of the array. see : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

Edan Feiles
  • 465
  • 4
  • 16