Monday, December 4, 2023
 Popular · Latest · Hot · Upcoming
130
rated 0 times [  132] [ 2]  / answers: 1 / hits: 34463  / 15 Years ago, mon, may 25, 2009, 12:00:00

Is it a standard way to assign to multiple variables from an array in JavaScript?
In Firefox and Opera, you can do:



var [key, value] = key:value.split(:);
alert(key + = + value); // will alert key = value;


But it doesn't work in IE8 or Google Chrome.



Does anyone know a nice way to do this in other browsers without a tmp variable?



var tmp = key:value.split(:);
var key=tmp[0], value=tmp[1];


Is this something that will come in an upcoming JavaScript version, or just custom implementation in FF and Opera?


More From » javascript

 Answers
35

If you want to know what's coming, read the section on Destructuring Assignment.



https://developer.mozilla.org/en/New_in_javascript_1.7



What language features you can use is always dependent on your environment.



Developing for Mobile Safari (or a web stack for other browsers like the Palm Pre, Android, etc.) or AIR, for example, is more predictable than developing for the web at large (where you still have to take even IE6 into account).






A cross-browser solution to the problem at hand would be to initialize an array that had a list of the variables you want to fill in window.variable format, then just loop through. Can't imagine why you'd do it though. Seems like there would always be a better solution.


[#99460] Wednesday, May 20, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
davisbrandtm

Total Points: 387
Total Questions: 99
Total Answers: 106

Location: Tuvalu
Member since Sat, Feb 11, 2023
11 Months ago
;