Skip to content

Instantly share code, notes, and snippets.

@srishanbhattarai
Created July 28, 2018 16:13
Show Gist options
  • Save srishanbhattarai/56fb330bcad5de21c742c68d959339e4 to your computer and use it in GitHub Desktop.
Save srishanbhattarai/56fb330bcad5de21c742c68d959339e4 to your computer and use it in GitHub Desktop.
Create an array using a map function
/**
* Create an array of length 'N' by executing repeaterFn for each item.
* This is useful where a method like `getItem()` can return a random item,
* and it can be repeated over N times.
*
* @param {function} repeaterFn
* @param {number} N=5
* @returns {Array}
*/
const repeat = (repeaterFn, N = 5) => Array(...Array(N)).map(repeaterFn)
export default repeat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment