Is there a Javascript equivalen of Perl's qw() method to quickly create arrays ? i.e.
in Perl @myarray = qw / one two three /;
in Javascript var myarray = ('one', 'two', 'three' ); // any alternative??
Is there a Javascript equivalen of Perl's qw() method to quickly create arrays ? i.e.
in Perl @myarray = qw / one two three /;
in Javascript var myarray = ('one', 'two', 'three' ); // any alternative??
var array:Array = [ 1 , 2 , 3 ];
var dictionary:Object = { a:1 , b:2 , c:3 };
There is not a built in construct, but you can do either of the following:
var myarray = 'one two three'.split(' '); // splits on single spaces
or
function qw (str) {return str.match(/\S+/g)}
var myarray = qw(' one two three '); // extracts words
To ‘quickly’ write an array, you can do this:
var x = 'foo bar baz'.split(' ');
Especially for large arrays, this is slightly easier to type than:
var x = ['foo', 'bar', 'baz'];
Although obviously, using .split()
is much less performant than just writing out the entire array.