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?
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.
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.
reverse() , i.e. reverses the array immutably.
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
...
}
You can do this by using the es6 spread operator now too:
let colors = [ ...inc_colors ].reverse()
Just make a copy of the array using Array.slice
(safe way):
var colors = Array.prototype.slice.call(inc_colors);
clean simple way that you may consider , but involves creating a new instance of the array is
var arr_reverse=arr.slice(0).reverse();
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();
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With