Javascript。将数组值分配给多个变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6983026/
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-08-24 00:07:12  来源:igfitidea点击:

Javascript. Assign array values to multiple variables?

javascriptarraysvariable-assignment

提问by tsds

var a,b,c;
var arr = [1,2,3];
[a,b,c] = arr;

this code works perfectly in Firefox resulting a=1, b=2 and c=3,
but it doesn't work in Chrome. Is it a Chrome bug or
it is not valid javascript code? (I failed to find it in javascript references)

这段代码在 Firefox 中完美运行,结果 a=1、b=2 和 c=3,
但在 Chrome 中不起作用。是 Chrome 错误还是
无效的 javascript 代码?(我没有在 javascript 参考资料中找到它)

How can I modify this code to make it suitable for Chrome, with minimum damage to it?
(I don't really like to write a = arr[0]; b = arr[1]... or the same with arr.shift() all the time)

如何修改此代码以使其适用于 Chrome,同时对它的损害最小?
(我真的不喜欢写 a = arr[0]; b = arr[1]... 或者一直用 arr.shift() 写一样)

P.S. this is just an example code, in real code
I get the arr array from somewhere outside my code

PS 这只是一个示例代码,在实际代码中,
我从代码之外的某个地方获取了 arr 数组

回答by Justin Ethier

This is a new feature of JavaScript 1.7 called Destructuring assignment:

这是 JavaScript 1.7 的一个新特性,称为Destructuring assignment

Destructuring assignment makes it possible to extract data from arrays or objects using a syntax that mirrors the construction of array and object literals.

The object and array literal expressions provide an easy way to create ad-hoc packages of data. Once you've created these packages of data, you can use them any way you want to. You can even return them from functions.

One particularly useful thing you can do with destructuring assignment is to read an entire structure in a single statement, although there are a number of interesting things you can do with them, as shown in the section full of examples that follows.

You can use destructuring assignment, for example, to swap values:

var a = 1;
var b = 3;
[a, b] = [b, a];

This capability is similar to features present in languages such as Perl and Python.

解构赋值可以使用反映数组和对象字面量构造的语法从数组或对象中提取数据。

对象和数组文字表达式提供了一种创建临时数据包的简单方法。创建这些数据包后,您可以以任何方式使用它们。你甚至可以从函数中返回它们。

您可以使用解构赋值做的一项特别有用的事情是在单个语句中读取整个结构,尽管您可以使用它们做许多有趣的事情,如下面的完整示例部分所示。

例如,您可以使用解构赋值来交换值:

var a = 1;
var b = 3;
[a, b] = [b, a];

此功能类似于 Perl 和 Python 等语言中存在的功能。

Unfortunately, according to this table of versions, JavaScript 1.7 has not been implemented in Chrome. But it should be there in:

不幸的是,根据这个版本表,JavaScript 1.7 尚未在 Chrome 中实现。但它应该在:

  • FireFox 2.0+
  • IE 9
  • Opera 11.50.
  • 火狐 2.0+
  • 浏览器 9
  • 歌剧 11.50。

Try it for yourself in this jsfiddle: http://jsfiddle.net/uBReg/

在这个 jsfiddle 中自己尝试一下:http: //jsfiddle.net/uBReg/

I tested this on Chrome (failed), IE 8 (failed), and FireFox 5 (which worked, per the wiki table).

我在 Chrome(失败)、IE 8(失败)和 FireFox 5(根据 wiki 表有效)上对此进行了测试。

回答by Jiri Kriz

It is possible only for Javascript 1.7 as already answered by @Justin. Here is a trial to simulate it in the widespread browsers:

正如@Justin 已经回答的那样,仅适用于 Javascript 1.7。这是在广泛使用的浏览器中模拟它的试验:

function assign(arr, vars) {
    var x = {};
    var num = Math.min(arr.length, vars.length);
    for (var i = 0; i < num; ++i) {
        x[vars[i]] = arr[i];
    }
    return x;
}
var arr = [1, 2, 3];
var x = assign(arr, ['a', 'b', 'c']);
var z = x.a + x.b + x.c;  // z == 6

I don't know how useful it is.

我不知道它有多大用处。