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;
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.
javascript
marked as duplicate by VLAZ, Mark Meyer
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.
add a comment |
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.
javascript
marked as duplicate by VLAZ, Mark Meyer
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
add a comment |
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.
javascript
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
javascript
asked Mar 25 at 14:47
LemileLemile
203 bronze badges
203 bronze badges
marked as duplicate by VLAZ, Mark Meyer
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
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
add a comment |
.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
add a comment |
4 Answers
4
active
oldest
votes
You can use the Array#fill()
method. For example, to create an array of size 12 entirely filled up with 10
s, you can use the following:
const array = new Array(12).fill(10)
console.log(array)
Here's some more documentation on this method.
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
add a comment |
If you just need to loop over the arguments passed into the function you can access them with the arguments keyword.
I didn't pass any arguments this time, but I'll try using it next time. :)
– Lemile
Mar 25 at 15:03
add a comment |
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)
Thank you so much!! Helped a lot :)
– Lemile
Mar 25 at 15:18
add a comment |
You could do the following
var temp = new Array(1000);
var count = temp.fill(0);
Thank you too :))))
– Lemile
Mar 25 at 15:18
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the Array#fill()
method. For example, to create an array of size 12 entirely filled up with 10
s, you can use the following:
const array = new Array(12).fill(10)
console.log(array)
Here's some more documentation on this method.
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
add a comment |
You can use the Array#fill()
method. For example, to create an array of size 12 entirely filled up with 10
s, you can use the following:
const array = new Array(12).fill(10)
console.log(array)
Here's some more documentation on this method.
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
add a comment |
You can use the Array#fill()
method. For example, to create an array of size 12 entirely filled up with 10
s, you can use the following:
const array = new Array(12).fill(10)
console.log(array)
Here's some more documentation on this method.
You can use the Array#fill()
method. For example, to create an array of size 12 entirely filled up with 10
s, 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)
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
add a comment |
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
add a comment |
If you just need to loop over the arguments passed into the function you can access them with the arguments keyword.
I didn't pass any arguments this time, but I'll try using it next time. :)
– Lemile
Mar 25 at 15:03
add a comment |
If you just need to loop over the arguments passed into the function you can access them with the arguments keyword.
I didn't pass any arguments this time, but I'll try using it next time. :)
– Lemile
Mar 25 at 15:03
add a comment |
If you just need to loop over the arguments passed into the function you can access them with the arguments keyword.
If you just need to loop over the arguments passed into the function you can access them with the arguments keyword.
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
add a comment |
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
add a comment |
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)
Thank you so much!! Helped a lot :)
– Lemile
Mar 25 at 15:18
add a comment |
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)
Thank you so much!! Helped a lot :)
– Lemile
Mar 25 at 15:18
add a comment |
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)
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)
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
add a comment |
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
add a comment |
You could do the following
var temp = new Array(1000);
var count = temp.fill(0);
Thank you too :))))
– Lemile
Mar 25 at 15:18
add a comment |
You could do the following
var temp = new Array(1000);
var count = temp.fill(0);
Thank you too :))))
– Lemile
Mar 25 at 15:18
add a comment |
You could do the following
var temp = new Array(1000);
var count = temp.fill(0);
You could do the following
var temp = new Array(1000);
var count = temp.fill(0);
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
add a comment |
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
add a comment |
.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