在 JavaScript 中将数组解包为单独的变量

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

Unpacking array into separate variables in JavaScript

javascriptarrays

提问by Carson Myers

This is a simple problem, and I've done it before. I just can't remember how, or what exactly it was called.

这是一个简单的问题,我以前做过。我只是不记得它是如何命名的,或者它到底叫什么。

In python I can do this:

在 python 中,我可以这样做:

arr = ['one', 'two']
one, two = arr

how do I do that in JavaScript?

我如何在 JavaScript 中做到这一点?

回答by Mathias Bynens

This is currently the only cross-browser-compatible solution AFAIK:

这是目前唯一的跨浏览器兼容解决方案 AFAIK:

var one = arr[0],
    two = arr[1];


ES6 will allow destructuring assignment:

ES6 将允许解构赋值:

let [x, y] = ['foo', 'bar'];
console.log(x); // 'foo'
console.log(y); // 'bar'

Or, to stick to your initial example:

或者,坚持你最初的例子:

var arr = ['one', 'two'];
var [one, two] = arr;

You could also create a default value:

您还可以创建一个默认值:

const [one = 'one', two = 'two', three = 'three'] = [1, 2];
console.log(one); // 1
console.log(two); // 2
console.log(three); // 'three'

回答by Andy E

That's destructuring assignment. You can do it in some browsers with the following syntax:

这就是解构赋值。您可以使用以下语法在某些浏览器中执行此操作:

[one, two] = arr;

It's supported in some of the latest browsers and transpilers like Babeland Traceur. This was a feature introduced with ECMAScript 4 which later became ECMAScript Harmony, which eventually became ES 2015.

它在一些最新的浏览器和转译器(如BabelTraceur )中得到支持。这是 ECMAScript 4 引入的一项功能,后来成为 ECMAScript Harmony,最终成为 ES 2015。

回答by Chris K.

The question is rather old but I like to post this alternative (2016) solution: One can also use the spreadoperator "...".

这个问题很老,但我喜欢发布这个替代(2016)解决方案:也可以使用扩展运算符“...”。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator

let xAndY = [42, 1337];
let f = function(x, y) { return x + y; };
f(...xAndY);

回答by serious

You can use array's apply function if you want an array items to be passed as a function arguments.

如果您希望将数组项作为函数参数传递,则可以使用数组的 apply 函数。

回答by Richard Ayotte

Implementation of serious's idea.

认真贯彻落实。

http://jsfiddle.net/RichAyotte/6D2wP/

http://jsfiddle.net/RichAyotte/6D2wP/

(function(a, b, c, d) {
    console.log(a, b, c, d);   
}.apply(this, ['a', 'b', 'c', 'd']));

回答by rob waminal

var one = arr[0];
var two = arr[1];

回答by Jakob

CoffeeScript has it: http://jashkenas.github.com/coffee-script/#pattern_matching

CoffeeScript 有它:http: //jashkenas.github.com/coffee-script/#pattern_matching

And, quoted from the top of the page:

并且,引自页面顶部:

"CoffeeScript is a little language that compiles into JavaScript. Think of it as JavaScript's less ostentatious kid brother — the same genes, roughly the same height, but a different sense of style. Apart from a handful of bonus goodies, statements in CoffeeScript correspond one-to-one with their equivalent in JavaScript, it's just another way of saying it."

“CoffeeScript 是一种编译成 JavaScript 的小语言。把它想象成 JavaScript 不那么炫耀的小兄弟——同样的基因,大致相同的高度,但风格不同。除了一些额外的好处之外,CoffeeScript 中的语句对应一个与 JavaScript 中的等价物一一对应,这只是另一种说法。”