Creating an independent copy of the reverse array in JavaScript

Here is my fiddle: http://jsfiddle.net/sepoto/Zgu9J/1/

I start with the inverse function:

function reverseArr(input) { var ret = new Array; for(var i = input.length-1; i >= 0; i--) { ret.push(input[i]); } //I tried changing the return value to //return ret.slice(0) which has no effect on making //an independent copy return ret; } 

The second array I make pointOrigins2 is not an independent copy of pointOrigins1. In other words, modifying pointOrigins2 also modifies pointOrigins1, which I should not achieve. From my reading on StackOverflow, I tried several options, such as using a fragment or using a for loop, but nothing seems to work, so I made a fiddle.

Is there a way to make an independent copy of the reverse array?

+2
javascript arrays reverse
May 14 '14 at 23:11
source share
5 answers

You create a new independent array, but you do not make independent copies of the elements that populate your arrays. You need to do something like:

 function reverseArr(input) { var ret = new Array; for(var i = input.length-1; i >= 0; i--) { ret.push({ positionx: input[i].positionx, positiony: input[i].positiony, index: input[i].index }); } return ret; } 

so that you generate new objects (with the same properties) as well as a new array.

+2
May 14 '14 at 23:14
source share

All you have to do is clone your array before you cancel it! :)

The shortest input method:

 var b = a.slice(); 

But using concat is also true. Cm:

The fastest Javascript way to duplicate an array is slice versus loop

If the slice does not create a copy for you, you can use numpy arrays and create a deep copy as follows:

 np.copy(a); 

see http://docs.scipy.org/doc/numpy/reference/generated/numpy.copy.html

+1
May 14 '14 at 23:14
source share

use this function

 function reverseArray(input) { var newArray = new Array(); for(var i = input.length-1; i >= 0; i--) { newArray.push(input[i]); } return newArray; } 
+1
May 15 '14 at 9:33
source share

Your function works correctly even if it can be reduced to

 return input.slice().reverse(); 



 //This will show that pointOrigins2 is not an independent copy of pointOrigins1 //When pointOrigins2 is modified pointOrigins1 is also being modified pointOrigins2[0].index++; 

No. You do not change the pointOrigins2 array here, you access it and change the point object - the same point as in the pointOrigins1 array. However, pointOrigins1 !== pointOrigins2 .

You can change the array independent of another, for example. as

 pointOrigins2[0] = { positionx: pointOrigins2[0].positionx, positiony: pointOrigins2[0].positiony, index: pointOrigins2[0].index + 1 }; 

and pointOrigins1 will remain untouched. Therefore, if you want to have points whose properties you can change without reflection elsewhere, you will need to create new points.

0
May 14 '14 at 23:21
source share
 function reverseArr(input) { return input.slice().reverse(); } 
0
May 14 '14 at 23:23
source share



All Articles