最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

perl - Javascript quick array declaration - Stack Overflow

programmeradmin5浏览0评论

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??
Share Improve this question asked Apr 10, 2010 at 4:34 Lydon ChLydon Ch 8,81520 gold badges80 silver badges136 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

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.

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
var array:Array = [ 1 , 2 , 3 ];
var dictionary:Object = { a:1 , b:2 , c:3 };
发布评论

评论列表(0)

  1. 暂无评论