Reputation: 22188
I am pretty sure the function exists in LoDash, but I can't find it in the doc:
_.complete([el1, el2], 5, 0) // [el1, el2, 0, 0, 0]
A method that would complete an array until a certain length
, with a certain value
.
Does it?
Thanks
Upvotes: 4
Views: 3516
Reputation: 12382
There is _.fill
, but it's not exactly what you want because it doesn't change the array length. You can, however, use it in combination with _.assign
:
_.assign(_.fill(new Array(5), 0), ["a", "b"])
// returns ["a", "b", 0, 0, 0]
Note though that this returns a new array instead of mutating the existing one.
If you want to mutate the array, you can do this:
function complete(arr, val, length) {
var oldLength = arr.length;
arr.length = length;
return _.fill(arr, 0, oldLength);
}
Upvotes: 11