Javascript fast array declaration

Is there a Javascript equivalent of the Perl qw () method for quickly creating arrays? those.

in Perl @myarray = qw / one two three /; in Javascript var myarray = ('one', 'two', 'three' ); // any alternative?? 
+4
source share
3 answers

To quickly write an array, you can do this:

 var x = 'foo bar baz'.split(' '); 

Especially for large arrays it is a little easier to print than:

 var x = ['foo', 'bar', 'baz']; 

Although, obviously, using .split() much less significant than just writing the entire array.

+6
source

There is no built-in design, but you can do one 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 
+4
source
 var array:Array = [ 1 , 2 , 3 ]; var dictionary:Object = { a:1 , b:2 , c:3 }; 
-2
source

Source: https://habr.com/ru/post/1306540/


All Articles