Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript. Assign array values to multiple variables? [duplicate]

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)

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)

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

like image 886
tsds Avatar asked Aug 08 '11 13:08

tsds


People also ask

How do you set multiple values in an array?

Use the spread syntax to push multiple values to an array, e.g. arr = [... arr, 'b', 'c', 'd']; . The spread syntax can be used to unpack the values of the original array followed by one or more additional values.

Can two variables have the same value JavaScript?

To assign multiple variables to the same value with JavaScript, we can use the destructuring syntax. For instance, we can write: const [ moveUp, moveDown, moveLeft, moveRight, mouseDown, touchDown ] = Array(6).

How do you assign the same value to multiple variables in typescript?

Put the varible in an array and Use a for Loop to assign the same value to multiple variables.

Does JavaScript have multiple assignment?

“JavaScript allows multiple left-hand assignments” var a = b = c = d = 10; The assignment expressions are always evaluated from right-to-left. So what the above expression actually does is, assign the value 10 to the variable d , then assign the value of d to c and so on.


1 Answers

This is a new feature of JavaScript 1.7 called 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.

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

  • FireFox 2.0+
  • IE 9
  • Opera 11.50.

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

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

like image 128
Justin Ethier Avatar answered Oct 05 '22 13:10

Justin Ethier