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
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With