4

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]);
Community
  • 1
  • 1
Bidoubiwa
  • 910
  • 2
  • 12
  • 25

5 Answers5

9

You can use Array.prototype.concat() and Array.prototype.fill()

  1. Create an array with size 12 and fill 0 by using Array.prototype.fill()
  2. Then concatenate with existing array using Array.prototype.concat()

var a = ['hihi', 'haha'].concat(Array(12).fill(0));


document.write('<pre>' + JSON.stringify(a, null, 3) + '</pre>');
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
1

You could for instance use a solution from the answer you link to create an array of twelve zeroes, and then use the Array.prototype.concat function to create the array you need.

var zeroes = Array.apply(null, Array(12)).map(Number.prototype.valueOf, 0);
var laughters = ['hihi', 'haha'];
var combined = laughters.concat(zeroes);
wkillerud
  • 31
  • 2
1
function fillArray(value, len) {
  if (len == 0) return [];
  var a = [value];
  while (a.length * 2 <= len) a = a.concat(a);
  if (a.length < len) a = a.concat(a.slice(0, len - a.length));
  return a;
}

It doubles the array in each iteration, so it can create a really large array with few iterations.

Vinod Bhavnani
  • 2,185
  • 1
  • 16
  • 19
0

In short, take your array and perform a concat with the answer you mentioned.

var a = ['hihi','haha'].concat(Array.apply(null, Array(12)).map(Number.prototype.valueOf,0);)
Community
  • 1
  • 1
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Hey, all the propositions are working, but why should i use your method instead of ['hihi','haha'].concat(Array(12).fill(0)) is there a difference ? – Bidoubiwa Apr 26 '16 at 07:33
  • 1
    @user3089871, because sometimes is fill [not available](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill#Browser_compatibility). – Nina Scholz Apr 26 '16 at 07:35
0

Must de 0 be an int or a string? And why do you want to add 12 zeros? What do you have tried so far?

Just an easy sample,

var a = ['hi', 'ha'];
for(var i = 0; 14 > a.length; i++){
a.push(0);
}
Rick Bronger
  • 330
  • 1
  • 15
  • I was answering the original question. He added this later i see now. In the question he makes the mistake to use 12 instead of 14 because there already 2 strings in the array. The original question was: 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]; – Rick Bronger Apr 26 '16 at 07:49
  • He didnt provide any examples. Look at my first 3 questions. He didnt show any examples of code he already tried. He edited his orignal question after i added my answer. So thats why my answer is not usefull anymore. So its not fair to say that my answer isnt usefull. It was before he changed his original question. :) – Rick Bronger Apr 26 '16 at 12:47