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]);

5 Answers 5

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>');

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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.

Comments

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);)

2 Comments

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 ?
@user3089871, because sometimes is fill not available.
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);
}

2 Comments

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];
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. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.