Creating a filled array with a given length?

# Axel Rauschmayer (13 years ago)

I would still love to have something like that in ES6 (loosely similar to String.prototype.repeat). Once you have that, you can e.g. use Array.prototype.map to do more things.

Two possibilities:

  • Array.repeat(undefined, 3) -> [ undefined, undefined, undefined ]
  • [ undefined ].repeat(3) -> [ undefined, undefined, undefined ]

The same array could be created like this, but that seems too much work for a relatively common operation.

'x'.repeat(3).split('').map(=> undefined)

# Rick Waldron (13 years ago)

On Wed, Dec 12, 2012 at 1:59 AM, Axel Rauschmayer <axel at rauschma.de> wrote:

I would still love to have something like that in ES6 (loosely similar to String.prototype.repeat). Once you have that, you can e.g. use Array.prototype.map to do more things.

Two possibilities:

  • Array.repeat(undefined, 3) -> [ undefined, undefined, undefined ]
  • [ undefined ].repeat(3) -> [ undefined, undefined, undefined ]

The same array could be created like this, but that seems too much work for a relatively common operation.

'x'.repeat(3).split('').map(=> undefined)

Array Comprehensions!

This is probably wrong, so treat it more like an idea and less like a matter of fact:

[ undefined for x of new Array(3) ].map( v => ... );

[ undefined for x of [0,0,0] ].map( v => ... );

# Herby Vojčík (13 years ago)

Rick Waldron wrote:

On Wed, Dec 12, 2012 at 1:59 AM, Axel Rauschmayer <axel at rauschma.de <mailto:axel at rauschma.de>> wrote:

I would still love to have something like that in ES6 (loosely
similar to String.prototype.repeat). Once you have that, you can
e.g. use Array.prototype.map to do more things.

Two possibilities:
- Array.repeat(undefined, 3) -> [ undefined, undefined, undefined ]
- [ undefined ].repeat(3) -> [ undefined, undefined, undefined ]

Array Comprehensions!

This is probably wrong, so treat it more like an idea and less like a matter of fact:

[ undefined for x of new Array(3) ].map( v => ... );

[ undefined for x of [0,0,0] ].map( v => ... );

Well, for the few occasions I did something in Python, I did it this way, but with range(3) in place of new Array(3).

If it's not a sin to do it this way, then please allow it by adding range (however called) in ES6.

Rick

Herby

P.S.: Or allow it some different way, it depends. integers().limit(3) is fine, too; if having infinite generator of increasing integers and decorator stopping after n items is better way.

P.P.S.: This open lots of ways. It can as well be [...infiniteStreamOf(undefined).limit(3)] (lotsOf(...) is shorter).

P.P.P.S.: That said, looping(a,b,c) generating a,b,c in a neverending loop (for any number of parameters) would be a good building block.

# Andrea Giammarchi (13 years ago)

I haven't seen anything similar to Array#unique yet which is much more common operation ( see the needing of Set ) and you are suggesting this that in my experience has basically no use cases except for zero filled flat matrixes ? :-)

function repeat(times) { var out = [], i = parseInt(times, 10); while (i--) out[i] = this ; return out.concat.apply([], out); }

repeat.call([1,2,3], 2); // => 1,2,3,1,2,3

for ES3 like environment ;)

br

# Axel Rauschmayer (13 years ago)

+1

A range function would be a great (possibly better) alternative, especially if combined with array comprehensions:

 let filled = [ myValue for _ of Array.range(1,3) ]

But we are getting into standard library territory again (after ES6 the only remaining Python advantage, IMO). There is remarkably little competition in this space. I’m wondering at what point Underscore.js can be considered proven enough to add its functionality to the language proper. Or is there something else that can be done to figure out what’s still missing from the standard library? Then we still need a more comprehensive collection library.

# Jussi Kalliokoski (13 years ago)

On Wed, Dec 12, 2012 at 6:08 PM, Rick Waldron <waldron.rick at gmail.com>wrote:

On Wed, Dec 12, 2012 at 1:59 AM, Axel Rauschmayer <axel at rauschma.de>wrote:

I would still love to have something like that in ES6 (loosely similar to String.prototype.repeat). Once you have that, you can e.g. use Array.prototype.map to do more things.

Two possibilities:

  • Array.repeat(undefined, 3) -> [ undefined, undefined, undefined ]
  • [ undefined ].repeat(3) -> [ undefined, undefined, undefined ]

The same array could be created like this, but that seems too much work for a relatively common operation.

'x'.repeat(3).split('').map(=> undefined)

Array Comprehensions!

This is probably wrong, so treat it more like an idea and less like a matter of fact:

[ undefined for x of new Array(3) ].map( v => ... );

[ undefined for x of [0,0,0] ].map( v => ... );

This should work, too (at least it works in Firefox, if you use a normal function):

var powersOfTwo = [ ...Array(8) ].map( (v, i) => 1 << i )

# Rick Waldron (13 years ago)

On Thu, Dec 13, 2012 at 4:14 AM, Jussi Kalliokoski < jussi.kalliokoski at gmail.com> wrote:

[ ...Array(8) ].map( (v, i) => 1 << i )

That's awesome, check it out: traceur-compiler.googlecode.com/git/demo/repl.html# [ ...Array(8) ].map( (v%2C i) %3D> 1 << i )