javascript 将反向数组保存到javascript中的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14366556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:27:15  来源:igfitidea点击:

Saving reversed array to variable in javascript

javascriptarraysreverse

提问by bozdoz

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?

如何在不更改全局数组的情况下获得反向全局数组?

回答by jfriend00

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).

当您将 javascript 中的数组或对象分配给变量时,它会分配对该数组/对象的引用。它不会复制数组/对象。因此,那么您将有两个变量都指向同一个数组/对象,修改其中一个会影响另一个(因为它们都指向相同的底层数据)。

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:

如果您想制作副本,则必须明确制作副本(javascript 不会自动为您完成)。对于一个数组,最简单的做一个浅拷贝的方法是这样的:

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
  ...
}

回答by Ian

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

您现在也可以使用 es6 扩展运算符来执行此操作:

let colors = [ ...inc_colors ].reverse()

回答by yeahdixon

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

您可能会考虑的干净简单的方法,但涉及创建数组的新实例是

var arr_reverse=arr.slice(0).reverse();

回答by VisioN

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

只需使用Array.slice(安全方式)复制数组:

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

回答by Gabriel Gartz

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();
  // ...
}