_.fill

Fills elements of array with value from start up to, but not including, end.Note that fill is a mutable method in both native and Lodash/Underscore.

  1. // Underscore/Lodash
  2. var array = [1, 2, 3]
  3.  
  4. _.fill(array, 'a')
  5.  
  6. console.log(array)
  7. // output: ['a', 'a', 'a']
  8.  
  9. _.fill(Array(3), 2)
  10. // output: [2, 2, 2]
  11.  
  12. _.fill([4, 6, 8, 10], '*', 1, 3)
  13. // output: [4, '*', '*', 10]
  14.  
  15. // Native
  16. var array = [1, 2, 3]
  17.  
  18. array.fill('a')
  19.  
  20. console.log(array)
  21. // output: ['a', 'a', 'a']
  22.  
  23. Array(3).fill(2)
  24. // output: [2, 2, 2]
  25.  
  26. [4, 6, 8, 10].fill('*', 1, 3)
  27. // output: [4, '*', '*', 10]

Browser Support for Array.prototype.fill()

ChromeEdgeFirefoxIEOperaSafari
45.0 ✔31.0 ✔32.0 ✔8 ✔