Single value array initialization

Is there a more compact way to do this initialization?

for (var i = 0; i < arraySize; i++) array[i] = value; 
+56
javascript
Oct 29 '10 at 7:27
source share
10 answers
 while(arraySize--) array.push(value); 

no initialization (what i know)




Update

Since you posted this answer 4 years ago, people seem to keep coming back here for this answer. For benchmarking purposes, I made JSPerf with some different solutions.

The solution above is not the fastest, although it is short. To adhere to the same short style, but with better performance:

 while(size--) array[size] = value; 



Update February 2016 Updated JSPerf with a new version with a large number of test boxes.

If performance doesn't matter and you want a single line:

 var value = 1234, // can be replaced by a fixed value size = 1000, // can be replaced by a fixed value array = Array.apply(null,{length: size}).map(function() { return value; }); 

A more efficient solution (in one, dirty, line): Remember: this replaces the existing values, size and variables i in the area

 for(var i = 0, value = 1234, size = 1000, array = new Array(1000); i < size; i++) array[i] = value; 
+36
Oct 29 '10 at 7:34
source share

One short way to do this:

 var arr = Array(arraySize).fill(value); 

Would do arr = Array [ 0, 0, 0, 0, 0 ] if arraySize == 5 and value == 0 , for example.

+91
Feb 13 '15 at 19:54
source share

It seems that after compactness in a one-time scenario, the effectiveness of OP and reuse. For others seeking efficiency, here is an optimization that has not yet been mentioned. Since you know the length of the array in advance, go ahead and set it before assigning values. Otherwise, the array will repeatedly change "on the fly" - not perfect!

 function initArray(length, value) { var arr = [], i = 0; arr.length = length; while (i < length) { arr[i++] = value; } return arr; } var data = initArray(1000000, false); 
+9
Oct 22 '12 at 3:19
source share

It is not so compact, but perhaps more direct.

 array = Array.apply(null, new Array(arraySize)).map(function () {return value;}); 
+7
Dec 07 '12 at 19:17
source share

It is unlikely to be better than any of the above methods, but it's fun ...

 var a = new Array(10).join('0').split('').map(function(e) {return parseInt(e, 10);}) 
+5
Oct 27 '12 at 2:24
source share

For efficiency, I would avoid push . So simple

 for (var i = 0; i < arraySize; i++) array[i] = value; 

For IE10:

 array = new Array(arraySize); for (var i = 0; i < arraySize; i++) array[i] = value; 

Edit: modified as described in the comments.

+2
Oct 05
source share

If you need to do this many times, you can always write a function:

 function makeArray(howMany, value){ var output = []; while(howMany--){ output.push(value); } return output; } var data = makeArray(40, "Foo"); 

And just for completeness (fiddling with a prototype of embedded objects is often not very good):

 Array.prototype.fill = function(howMany, value){ while(howMany--){ this.push(value); } } 

So now you can:

 var data = []; data.fill(40, "Foo"); 



Update: I just saw your note about arraySize as a constant or literal. If so, just replace all while(howMany--) with the good old for(var i=0; i<howMany; i++) .

+1
Oct 29 '10 at 8:05
source share

Stumbled upon this while exploring array methods on a plane. Oh, the places we go when we're bored. :)

 var initializedArray = new Array(30).join(null).split(null).map(function(item, index){ return index; }); 

.map() and null to win! I like null because switching to a string of type up with "0" or any other value is confusing. I think it is more obvious that we are doing something else.

Note that .map() skips uninitialized values. This is why new Array(30).map(function(item, index){return index}); does not work. The new .fill() method is preferred if available, however browser support should be marked as 8/23/2015 .

Desktop (basic support)

  • Chrome 45 (36 1 )
  • Firefox (Gecko) 31 (31)
  • Internet Explorer is not supported
  • Opera is not supported
  • Safari 7.1

From MDN :

 [1, 2, 3].fill(4); // [4, 4, 4] [1, 2, 3].fill(4, 1); // [1, 4, 4] [1, 2, 3].fill(4, 1, 2); // [1, 4, 3] [1, 2, 3].fill(4, 1, 1); // [1, 2, 3] [1, 2, 3].fill(4, -3, -2); // [4, 2, 3] [1, 2, 3].fill(4, NaN, NaN); // [1, 2, 3] Array(3).fill(4); // [4, 4, 4] [].fill.call({ length: 3 }, 4); // {0: 4, 1: 4, 2: 4, length: 3} 
0
Aug 24 '15 at 3:05
source share

I think it is beautiful, short and elegant.

 // assuming 'value' is what you want to fill the array with // and 'arraySize' is the size of the array Array(arraySize).fill(value); 
0
Jun 14 '19 at 12:37
source share
 var a=Array(100).join("|").split("|").map(Number); 
-one
May 10 '17 at 12:16
source share



All Articles