Idiom for "repeat n times"?

shortmost elegant ES6:

let times = (n, f) => { while(n-- > 0) f(); }

oh, That's not for creating an array, but it's still neat!

times(3, () => print('wow'))

or Ruby style:

Object.assign(Number.prototype, { times(f) { x = this; while(x-- > 0) f(); }})
3..times(() => print('wow'))

Underscore.js has a times function that does exactly what you want:

_.times(3, Math.random)

If you don't want to use Underscore, you can just write your own times function (copied and slightly simplified from the Underscore source):

times = function(n, iterator) {
  var accum = Array(Math.max(0, n));
  for (var i = 0; i < n; i++) accum[i] = iterator.call();
  return accum;
};

It can be done using Array.prototype.map, but the array can't be empty. Fill it first:

console.log(
    Array(3).fill().map(Math.random)
);

Explanation:

The new Array(3) constructor creates a sparse array (or "holey" array, as the V8 team calls them) with three holes in it and a length of three. This means that it's equivalent to [,,,], which creates [<empty>, <empty>, <empty>,] (note JavaScript's trailing commas). Note that an empty slot, i.e. a hole is not the same as undefined as an assigned value. undefined is an actual value, whereas <empty> is just a gap in the array.

Array.prototype.map is called once for each element in the array. But, because an empty array has no assigned values, the callback doesn't get called at all. For example, [1,,2].map(v=>v*2) would give [2,,4]; the middle slot is skipped, as it has a gap there.

Enter Array.prototype.fill(value, start?, end?): with only one argument, it fills every slot in the array with the specified value. Technically, the first parameter is not optional, but by omitting it, undefined is used as the value. This is okay, because the value isn't being used anyway. This way Array(3).fill() gives us [undefined, undefined, undefined].

Now that the array has values in it, it can be mapped over, like seen above.


You could also spread the empty array into values of undefined before mapping:

console.log(
    [...Array(3)].map(Math.random)
);

Explanation:

Array operators introduced in ECMAScript2015 or newer treat holes in arrays as undefined values. Array.prototype.map was introduced in ES5 (I.E. what preceded ES2015), where, confusingly, holes in arrays are to be skipped over, creating a little bit of inconsistency in JS Array functions depending on which edition of ECMAScript they were released in.

The spread operator ... was introduced in ES2015, so as per spec, it converts any holes in the given array into values of undefined. In other words, [...Array(3)] gives us [undefined, undefined, undefined], just like Array(3).fill() did above.


Sometimes you may need to seed in numbers sequentially. As pointed out by Kevin Danikowski, Array.prototype.map gives you that out of the box, as the second parameter is the current key:

const Fibonacci = n => Math.round(((5**.5 + 1) / 2)**n / 5**.5);

console.log(
    Array(10).fill().map((_, i) => Fibonacci(++i))
);

Tags:

Javascript