Util

_.times

Invokes the iteratee n times, returning an array of the results of each invocation.

  1. // Lodash
  2. var result = _.times(10)
  3. console.log(result)
  4. // output: '[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]'
  5.  
  6. // Native
  7. var result = Array.from({length: 10}, (_,x) => x)
  8. console.log(result)
  9. // output: '[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]'
  10.  
  11. // Native
  12. var result = [...Array(10).keys()]
  13. console.log(result)
  14. // output: '[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]'

Browser Support for Array.from()

ChromeEdgeFirefoxIEOperaSafari
45.0 ✔32.0 ✔9.0 ✔