Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

For numbers you can use ES6 Array.from(), which works in everything these days except IE:

Shorter version:

Array.from({length: 20}, (x,i) => i);

Longer version:

Array.from(new Array(20), (x,i) => i);​​​​​​

which creates an array from 0 to 19 inclusive. This can be further shortened to one of these forms:

Array.from(Array(20).keys());
// or
[...Array(20).keys()]];

Lower and upper bounds can be specified too, for example:

Array.from(new Array(20), (x,i) => i + *lowerBound*);

An article describing this in more detail: http://www.2ality.com/2014/05/es6-array-methods.html

For numbers you can use ES6 Array.from(), which works in everything these days except IE:

Shorter version:

Array.from({length: 20}, (x,i) => i);

Longer version:

Array.from(new Array(20), (x,i) => i)

which creates an array from 0 to 19 inclusive. This can be further shortened to one of these forms:

Array.from(Array(20).keys())
// or
[...Array(20).keys()]

Lower and upper bounds can be specified too, for example:

Array.from(new Array(20), (x,i) => i + *lowerBound*)

An article describing this in more detail: http://www.2ality.com/2014/05/es6-array-methods.html

For numbers you can use ES6 Array.from(), which works in everything these days except IE:

Shorter version:

Array.from({length: 20}, (x,i) => i);

Longer version:

Array.from(new Array(20), (x,i) => i);​​​​​​

which creates an array from 0 to 19 inclusive. This can be further shortened to one of these forms:

Array.from(Array(20).keys());
// or
[...Array(20).keys()];

Lower and upper bounds can be specified too, for example:

Array.from(new Array(20), (x,i) => i + *lowerBound*);

An article describing this in more detail: http://www.2ality.com/2014/05/es6-array-methods.html

Wanted to give a example that is shorter with less operations
Source Link
dman
  • 11.1k
  • 25
  • 118
  • 217

For numbers you can use ES6 Array.from(), which works in everything these days except IE:

Shorter version:

Array.from({length: 20}, (x,i) => i);

Longer version:

Array.from(new Array(20), (x,i) => i)

which creates an array from 0 to 19 inclusive. This can be further shortened to one of these forms:

Array.from(Array(20).keys())
// or
[...Array(20).keys()]

Lower and upper bounds can be specified too, for example:

Array.from(new Array(20), (x,i) => i + *lowerBound*)

An article describing this in more detail: http://www.2ality.com/2014/05/es6-array-methods.html

For numbers you can use ES6 Array.from(), which works in everything these days except IE:

Array.from(new Array(20), (x,i) => i)

which creates an array from 0 to 19 inclusive. This can be further shortened to one of these forms:

Array.from(Array(20).keys())
// or
[...Array(20).keys()]

Lower and upper bounds can be specified too, for example:

Array.from(new Array(20), (x,i) => i + *lowerBound*)

An article describing this in more detail: http://www.2ality.com/2014/05/es6-array-methods.html

For numbers you can use ES6 Array.from(), which works in everything these days except IE:

Shorter version:

Array.from({length: 20}, (x,i) => i);

Longer version:

Array.from(new Array(20), (x,i) => i)

which creates an array from 0 to 19 inclusive. This can be further shortened to one of these forms:

Array.from(Array(20).keys())
// or
[...Array(20).keys()]

Lower and upper bounds can be specified too, for example:

Array.from(new Array(20), (x,i) => i + *lowerBound*)

An article describing this in more detail: http://www.2ality.com/2014/05/es6-array-methods.html

added 291 characters in body
Source Link
Mike Chamberlain
  • 43k
  • 28
  • 113
  • 160

For numbers you can soon use the ES6 array feature. Which currently only works in Firefox.Array.from(), which works in everything these days except IE:

Array.from(new Array(20), (x,i) => i)

Would createwhich creates an array from 0 to 19 inclusive. This can be further shortened to one of these forms:

Array.from(Array(20).keys())
// or
[...Array(20).keys()]

Lower and upper bounds can be addedspecified too, for example by:

Array.from(new Array(20), (x,i) => i + *lowerBound*)

An article describing itthis in more precisedetail: http://www.2ality.com/2014/05/es6-array-methods.html

For numbers you can soon use the ES6 array feature. Which currently only works in Firefox.

Array.from(new Array(20), (x,i) => i)

Would create an array from 0 to 19. Lower and upper bounds can be added for example by

Array.from(new Array(20), (x,i) => i + *lowerBound*)

An article describing it more precise: http://www.2ality.com/2014/05/es6-array-methods.html

For numbers you can use ES6 Array.from(), which works in everything these days except IE:

Array.from(new Array(20), (x,i) => i)

which creates an array from 0 to 19 inclusive. This can be further shortened to one of these forms:

Array.from(Array(20).keys())
// or
[...Array(20).keys()]

Lower and upper bounds can be specified too, for example:

Array.from(new Array(20), (x,i) => i + *lowerBound*)

An article describing this in more detail: http://www.2ality.com/2014/05/es6-array-methods.html

Source Link
Kristjan Liiva
  • 9.7k
  • 3
  • 28
  • 26
Loading
lang-js

AltStyle によって変換されたページ (->オリジナル) /