I found on this link how to create arrays with a chosen number of zeros. Most efficient way to create a zero filled JavaScript array?
But my question is, imagine i already have a array
var a = ['hihi','haha']
how can i add 12 zeros after my two first elements ? So that it becomes :
a = ['hihi','haha',0,0,0,0,0,0,0,0,0,0,0,0];
of course I could go for a
for(var i = 0; i < 12, i++){
a.push(0);
}
but is there a one line method ? something like
a.push(6*[0]);
You can use Array.prototype.concat()
and Array.prototype.fill()
12
and fill 0
by using Array.prototype.fill()
Array.prototype.concat()
var a = ['hihi', 'haha'].concat(Array(12).fill(0));
document.write('<pre>' + JSON.stringify(a, null, 3) + '</pre>');
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