Creates a new dense array with the given size, optionally filled with a value.
var array = require('new-array')
array(2)
// > [ undefined, undefined ]
array(4, 0)
// > [ 0, 0, 0, 0 ]
array()
// > []
// using a function to generate values
array(4, (i) => i * 10)
// > [0, 10, 20, 30]
Primarily motivated by the fact that new Array(n)
produces an array of holes that does not play well with methods like Array#map()
.
Returns a new dense array with length n
(default 0), where each element is set to value
, or fn(i)
if its a function, or undefined if not specified.
MIT, see LICENSE.md for details.