Smarter Way to Initialize Array in Javascript [duplicate]Most efficient way to create a zero filled JavaScript array?How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?Loop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?

Can a successful book series let the bad guy win?

Why isn't UDP with reliability (implemented at Application layer) a substitute of TCP?

Movie with Zoltar in a trailer park named Paradise and a boy playing a video game then being recruited by aliens to fight in space

Security Patch SUPEE-11155 - Possible issues?

Missing root certificates on Windows Server 2016 (fresh install)

How could an armless race establish civilization?

Can dual citizens open crypto exchange accounts where U.S. citizens are prohibited?

Translation of the Sator Square

On what to compliment someone with anorexia in order to improve their body image?

How did Lefschetz do mathematics without hands?

My colleague is constantly blaming me for his errors

Why was Pan Am Flight 103 flying over Lockerbie?

Closest Proximity of Oceans to Freshwater Springs

Can a stressful Wish's Strength reduction be cured early by a Greater Restoration spell?

Is there a legal way for US presidents to extend their terms beyond two terms of four years?

Sharing referee/AE report online to point out a grievous error in refereeing

What game is this character in the Pixels movie from?

What do you call a notepad used to keep a record?

Do home values typically rise and fall consistently across different price ranges?

How can I open this door latch with the knobs removed?

Conference in Los Angeles, visa?

Put my student loan in parents’ second mortgage - help?

Why do movie directors use brown tint on Mexico cities?

How can I deal with extreme temperatures in a hotel room?



Smarter Way to Initialize Array in Javascript [duplicate]


Most efficient way to create a zero filled JavaScript array?How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?Loop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0
















This question already has an answer here:



  • Most efficient way to create a zero filled JavaScript array?

    38 answers



I made this code to check out how "random" the random function is.



Array.prototype.random = function() 
var index = Math.floor(this.length*Math.random());
return this[index];

var arr = new Array('a', 'b', 'c', 'd', 'e');

var count = [0, 0, 0, 0, 0]; //I want to ask about this part

for (var i = 0; i < 10000; i++)
var val = arr.random();
console.log(val);

switch (val)
case 'a':
count[0]++;
break;
case 'b':
count[1]++;
break;
case 'c':
count[2]++;
break;
case 'd':
count[3]++;
break;
case 'e':
count[4]++;
break;



for (var i = 0; i < count.length; i++) console.log(count[i]);


In the process, I couldn't think of better way to initialize the 'count' array. Using that way, it would get really repetitive. I would have to put thousand 0 if my 'arr' has 1000 elements. Can anybody suggest better way for me?



If I don't initialize each element of 'count' array as 0, I get NaN.










share|improve this question













marked as duplicate by VLAZ, Mark Meyer javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 25 at 14:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • .fill() method?

    – zer00ne
    Mar 25 at 14:49











  • Thanks! I tried! I just had to declare 'count' array differently. var count = new Array(arr.length); count.fill(0, 0); but it worked!!

    – Lemile
    Mar 25 at 15:07


















0
















This question already has an answer here:



  • Most efficient way to create a zero filled JavaScript array?

    38 answers



I made this code to check out how "random" the random function is.



Array.prototype.random = function() 
var index = Math.floor(this.length*Math.random());
return this[index];

var arr = new Array('a', 'b', 'c', 'd', 'e');

var count = [0, 0, 0, 0, 0]; //I want to ask about this part

for (var i = 0; i < 10000; i++)
var val = arr.random();
console.log(val);

switch (val)
case 'a':
count[0]++;
break;
case 'b':
count[1]++;
break;
case 'c':
count[2]++;
break;
case 'd':
count[3]++;
break;
case 'e':
count[4]++;
break;



for (var i = 0; i < count.length; i++) console.log(count[i]);


In the process, I couldn't think of better way to initialize the 'count' array. Using that way, it would get really repetitive. I would have to put thousand 0 if my 'arr' has 1000 elements. Can anybody suggest better way for me?



If I don't initialize each element of 'count' array as 0, I get NaN.










share|improve this question













marked as duplicate by VLAZ, Mark Meyer javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 25 at 14:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • .fill() method?

    – zer00ne
    Mar 25 at 14:49











  • Thanks! I tried! I just had to declare 'count' array differently. var count = new Array(arr.length); count.fill(0, 0); but it worked!!

    – Lemile
    Mar 25 at 15:07














0












0








0


0







This question already has an answer here:



  • Most efficient way to create a zero filled JavaScript array?

    38 answers



I made this code to check out how "random" the random function is.



Array.prototype.random = function() 
var index = Math.floor(this.length*Math.random());
return this[index];

var arr = new Array('a', 'b', 'c', 'd', 'e');

var count = [0, 0, 0, 0, 0]; //I want to ask about this part

for (var i = 0; i < 10000; i++)
var val = arr.random();
console.log(val);

switch (val)
case 'a':
count[0]++;
break;
case 'b':
count[1]++;
break;
case 'c':
count[2]++;
break;
case 'd':
count[3]++;
break;
case 'e':
count[4]++;
break;



for (var i = 0; i < count.length; i++) console.log(count[i]);


In the process, I couldn't think of better way to initialize the 'count' array. Using that way, it would get really repetitive. I would have to put thousand 0 if my 'arr' has 1000 elements. Can anybody suggest better way for me?



If I don't initialize each element of 'count' array as 0, I get NaN.










share|improve this question















This question already has an answer here:



  • Most efficient way to create a zero filled JavaScript array?

    38 answers



I made this code to check out how "random" the random function is.



Array.prototype.random = function() 
var index = Math.floor(this.length*Math.random());
return this[index];

var arr = new Array('a', 'b', 'c', 'd', 'e');

var count = [0, 0, 0, 0, 0]; //I want to ask about this part

for (var i = 0; i < 10000; i++)
var val = arr.random();
console.log(val);

switch (val)
case 'a':
count[0]++;
break;
case 'b':
count[1]++;
break;
case 'c':
count[2]++;
break;
case 'd':
count[3]++;
break;
case 'e':
count[4]++;
break;



for (var i = 0; i < count.length; i++) console.log(count[i]);


In the process, I couldn't think of better way to initialize the 'count' array. Using that way, it would get really repetitive. I would have to put thousand 0 if my 'arr' has 1000 elements. Can anybody suggest better way for me?



If I don't initialize each element of 'count' array as 0, I get NaN.





This question already has an answer here:



  • Most efficient way to create a zero filled JavaScript array?

    38 answers







javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 25 at 14:47









LemileLemile

203 bronze badges




203 bronze badges




marked as duplicate by VLAZ, Mark Meyer javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 25 at 14:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by VLAZ, Mark Meyer javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 25 at 14:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • .fill() method?

    – zer00ne
    Mar 25 at 14:49











  • Thanks! I tried! I just had to declare 'count' array differently. var count = new Array(arr.length); count.fill(0, 0); but it worked!!

    – Lemile
    Mar 25 at 15:07


















  • .fill() method?

    – zer00ne
    Mar 25 at 14:49











  • Thanks! I tried! I just had to declare 'count' array differently. var count = new Array(arr.length); count.fill(0, 0); but it worked!!

    – Lemile
    Mar 25 at 15:07

















.fill() method?

– zer00ne
Mar 25 at 14:49





.fill() method?

– zer00ne
Mar 25 at 14:49













Thanks! I tried! I just had to declare 'count' array differently. var count = new Array(arr.length); count.fill(0, 0); but it worked!!

– Lemile
Mar 25 at 15:07






Thanks! I tried! I just had to declare 'count' array differently. var count = new Array(arr.length); count.fill(0, 0); but it worked!!

– Lemile
Mar 25 at 15:07













4 Answers
4






active

oldest

votes


















0














You can use the Array#fill() method. For example, to create an array of size 12 entirely filled up with 10s, you can use the following:






const array = new Array(12).fill(10)
console.log(array)





Here's some more documentation on this method.






share|improve this answer























  • Thank you!! It really helped

    – Lemile
    Mar 25 at 15:19











  • You're welcome :) If you want, accept the answer so others who come across your question can find it.

    – Kognise
    Mar 25 at 15:20


















0














If you just need to loop over the arguments passed into the function you can access them with the arguments keyword.






share|improve this answer























  • I didn't pass any arguments this time, but I'll try using it next time. :)

    – Lemile
    Mar 25 at 15:03


















0














I'm not exactly sure if that's what you're asking about, but if you want to initialize an array with 1000 same entries, all you need to do is:



var count = new Array(1000).fill(0)





share|improve this answer























  • Thank you so much!! Helped a lot :)

    – Lemile
    Mar 25 at 15:18


















0














You could do the following



var temp = new Array(1000);
var count = temp.fill(0);





share|improve this answer























  • Thank you too :))))

    – Lemile
    Mar 25 at 15:18



















4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can use the Array#fill() method. For example, to create an array of size 12 entirely filled up with 10s, you can use the following:






const array = new Array(12).fill(10)
console.log(array)





Here's some more documentation on this method.






share|improve this answer























  • Thank you!! It really helped

    – Lemile
    Mar 25 at 15:19











  • You're welcome :) If you want, accept the answer so others who come across your question can find it.

    – Kognise
    Mar 25 at 15:20















0














You can use the Array#fill() method. For example, to create an array of size 12 entirely filled up with 10s, you can use the following:






const array = new Array(12).fill(10)
console.log(array)





Here's some more documentation on this method.






share|improve this answer























  • Thank you!! It really helped

    – Lemile
    Mar 25 at 15:19











  • You're welcome :) If you want, accept the answer so others who come across your question can find it.

    – Kognise
    Mar 25 at 15:20













0












0








0







You can use the Array#fill() method. For example, to create an array of size 12 entirely filled up with 10s, you can use the following:






const array = new Array(12).fill(10)
console.log(array)





Here's some more documentation on this method.






share|improve this answer













You can use the Array#fill() method. For example, to create an array of size 12 entirely filled up with 10s, you can use the following:






const array = new Array(12).fill(10)
console.log(array)





Here's some more documentation on this method.






const array = new Array(12).fill(10)
console.log(array)





const array = new Array(12).fill(10)
console.log(array)






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 25 at 14:55









KogniseKognise

3162 silver badges14 bronze badges




3162 silver badges14 bronze badges












  • Thank you!! It really helped

    – Lemile
    Mar 25 at 15:19











  • You're welcome :) If you want, accept the answer so others who come across your question can find it.

    – Kognise
    Mar 25 at 15:20

















  • Thank you!! It really helped

    – Lemile
    Mar 25 at 15:19











  • You're welcome :) If you want, accept the answer so others who come across your question can find it.

    – Kognise
    Mar 25 at 15:20
















Thank you!! It really helped

– Lemile
Mar 25 at 15:19





Thank you!! It really helped

– Lemile
Mar 25 at 15:19













You're welcome :) If you want, accept the answer so others who come across your question can find it.

– Kognise
Mar 25 at 15:20





You're welcome :) If you want, accept the answer so others who come across your question can find it.

– Kognise
Mar 25 at 15:20













0














If you just need to loop over the arguments passed into the function you can access them with the arguments keyword.






share|improve this answer























  • I didn't pass any arguments this time, but I'll try using it next time. :)

    – Lemile
    Mar 25 at 15:03















0














If you just need to loop over the arguments passed into the function you can access them with the arguments keyword.






share|improve this answer























  • I didn't pass any arguments this time, but I'll try using it next time. :)

    – Lemile
    Mar 25 at 15:03













0












0








0







If you just need to loop over the arguments passed into the function you can access them with the arguments keyword.






share|improve this answer













If you just need to loop over the arguments passed into the function you can access them with the arguments keyword.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 25 at 14:50









Jordan AllainJordan Allain

12 bronze badges




12 bronze badges












  • I didn't pass any arguments this time, but I'll try using it next time. :)

    – Lemile
    Mar 25 at 15:03

















  • I didn't pass any arguments this time, but I'll try using it next time. :)

    – Lemile
    Mar 25 at 15:03
















I didn't pass any arguments this time, but I'll try using it next time. :)

– Lemile
Mar 25 at 15:03





I didn't pass any arguments this time, but I'll try using it next time. :)

– Lemile
Mar 25 at 15:03











0














I'm not exactly sure if that's what you're asking about, but if you want to initialize an array with 1000 same entries, all you need to do is:



var count = new Array(1000).fill(0)





share|improve this answer























  • Thank you so much!! Helped a lot :)

    – Lemile
    Mar 25 at 15:18















0














I'm not exactly sure if that's what you're asking about, but if you want to initialize an array with 1000 same entries, all you need to do is:



var count = new Array(1000).fill(0)





share|improve this answer























  • Thank you so much!! Helped a lot :)

    – Lemile
    Mar 25 at 15:18













0












0








0







I'm not exactly sure if that's what you're asking about, but if you want to initialize an array with 1000 same entries, all you need to do is:



var count = new Array(1000).fill(0)





share|improve this answer













I'm not exactly sure if that's what you're asking about, but if you want to initialize an array with 1000 same entries, all you need to do is:



var count = new Array(1000).fill(0)






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 25 at 14:53









Michał SadowskiMichał Sadowski

5643 silver badges12 bronze badges




5643 silver badges12 bronze badges












  • Thank you so much!! Helped a lot :)

    – Lemile
    Mar 25 at 15:18

















  • Thank you so much!! Helped a lot :)

    – Lemile
    Mar 25 at 15:18
















Thank you so much!! Helped a lot :)

– Lemile
Mar 25 at 15:18





Thank you so much!! Helped a lot :)

– Lemile
Mar 25 at 15:18











0














You could do the following



var temp = new Array(1000);
var count = temp.fill(0);





share|improve this answer























  • Thank you too :))))

    – Lemile
    Mar 25 at 15:18















0














You could do the following



var temp = new Array(1000);
var count = temp.fill(0);





share|improve this answer























  • Thank you too :))))

    – Lemile
    Mar 25 at 15:18













0












0








0







You could do the following



var temp = new Array(1000);
var count = temp.fill(0);





share|improve this answer













You could do the following



var temp = new Array(1000);
var count = temp.fill(0);






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 25 at 14:54









safwanmohasafwanmoha

1611 silver badge10 bronze badges




1611 silver badge10 bronze badges












  • Thank you too :))))

    – Lemile
    Mar 25 at 15:18

















  • Thank you too :))))

    – Lemile
    Mar 25 at 15:18
















Thank you too :))))

– Lemile
Mar 25 at 15:18





Thank you too :))))

– Lemile
Mar 25 at 15:18



Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현