Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript equivalent of PHP's list()

Really like that function.

$matches = array('12', 'watt'); list($value, $unit) = $matches; 

Is there a Javascript equivalent of that?

like image 362
Znarkus Avatar asked Dec 23 '09 18:12

Znarkus


1 Answers

There is, in 'newer' versions of Javascript: Destructuring assignment - Javascript 1.7. It's probably only supported in Mozilla-based browsers, and maybe in Rhino.

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

EDIT: actually it wouldn't surprise me if the V8 Javascript library (and thus Chrome) supports this. But don't count on it either Now supported in all modern browsers(except IE, of course).

like image 196
Nicolás Avatar answered Sep 30 '22 19:09

Nicolás