Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving reversed array to variable in javascript

I have an array of colors that I want the option to reverse. I have a toggle function that basically colors elements based on the array. If I throw a reverse variable then it reverses, but it reverses the global variable instead of the local variable.

var inc_colors = ['#000','#333','#888']; //global inc_colors

function toggleLegendColors(reverse){
  var reverse = reverse || false;
  var colors = inc_colors; //local colors
  if(reverse) colors.reverse(); //reverses inc_colors array as well as colors
  ...
}

How can I get the reversed global array without changing the global array?

like image 942
bozdoz Avatar asked Jan 16 '13 19:01

bozdoz


People also ask

How do you store an array in reverse?

Reverse a array(f.e. niz[] = { 2, 4, 5, 7, 4, 8, 3 } needs to become niz[] = { 3, 8, 4, 7, 5, 4, 2 } Store the values of a reversed array into a brand new array. All of it has to be done without using printf inside the function that reverses the array and stores the values into the new array.

How do you reverse an array without changing the original?

To reverse an array without modifying the original, call the slice() method on the array to create a shallow copy and call the reverse() method on the copy, e.g. arr. slice(). reverse() . The reverse method will not modify the original array when used on the copy.

Does reverse function changes the original array in JavaScript?

reverse() , i.e. reverses the array immutably.


5 Answers

Since nobody has really explained why you were having a problem, I'll add that to the mix.

When you assign an array or an object in javascript to a variable, it assigns a reference to that array/object. It does not make a copy of the array/object. So, then you would have two variables that both point at the same array/object and modifying either one will affect the other (since they both point to the same underlying piece of data).

So, when you had this:

var inc_colors = ['#000','#333','#888']; //global inc_colors
var colors = inc_colors; //local colors

All you have now is two variables that both point to the exact same piece of data. Modify either one and the same result will show via the other variable because they point to the same underlying data.

If you want to make a copy, then have to explicitly make a copy (javascript doesn't do it for you automatically). For an array, the simplest way to make a shallow copy is like this:

var newColors = Array.prototype.slice.call(inc_colors);

So, in your exact code, you could apply that copy like this:

var inc_colors = ['#000','#333','#888']; //global inc_colors

function toggleLegendColors(reverse){
  var reverse = reverse || false;
  var colors = Array.prototype.slice.call(inc_colors);  //local copy of the colors array
  if(reverse) colors.reverse(); //reverses inc_colors array as well as colors
  ...
}
like image 192
jfriend00 Avatar answered Oct 03 '22 03:10

jfriend00


You can do this by using the es6 spread operator now too:

let colors = [ ...inc_colors ].reverse()
like image 39
Ian Avatar answered Oct 03 '22 03:10

Ian


Just make a copy of the array using Array.slice (safe way):

var colors = Array.prototype.slice.call(inc_colors);
like image 44
VisioN Avatar answered Oct 03 '22 05:10

VisioN


clean simple way that you may consider , but involves creating a new instance of the array is

var arr_reverse=arr.slice(0).reverse();
like image 28
yeahdixon Avatar answered Oct 03 '22 05:10

yeahdixon


Simplistic solution:

var inc_colors = ['#000','#333','#888']; //global inc_colors

function toggleLegendColors(reverse) {
  var colors = (inc_colors instanceof Array) ? inc_colors : [];
  colors = (!reverse) ? inc_colors.slice() : inc_colors.slice().reverse();
  // ...
}
like image 33
Gabriel Gartz Avatar answered Oct 03 '22 05:10

Gabriel Gartz