How to add same elements to javascript array n times
up vote
6
down vote
favorite
var fruits = ;
fruits.push("lemon", "lemon", "lemon", "lemon");
Instead of pushing same elements how can write it once like this:
fruits.push("lemon" * 4 times)
javascript
add a comment |
up vote
6
down vote
favorite
var fruits = ;
fruits.push("lemon", "lemon", "lemon", "lemon");
Instead of pushing same elements how can write it once like this:
fruits.push("lemon" * 4 times)
javascript
They're not the same elements. The first one has uppercaseL
, the others have lowercasel
.
– Barmar
Aug 14 at 1:05
changed the typo
– Deke
Aug 14 at 1:08
Possible duplicate of Create an array with same element repeated multiple times
– Akrion
Oct 12 at 6:07
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
var fruits = ;
fruits.push("lemon", "lemon", "lemon", "lemon");
Instead of pushing same elements how can write it once like this:
fruits.push("lemon" * 4 times)
javascript
var fruits = ;
fruits.push("lemon", "lemon", "lemon", "lemon");
Instead of pushing same elements how can write it once like this:
fruits.push("lemon" * 4 times)
javascript
javascript
edited Aug 14 at 6:28
asked Aug 13 at 23:55
Deke
1,25611121
1,25611121
They're not the same elements. The first one has uppercaseL
, the others have lowercasel
.
– Barmar
Aug 14 at 1:05
changed the typo
– Deke
Aug 14 at 1:08
Possible duplicate of Create an array with same element repeated multiple times
– Akrion
Oct 12 at 6:07
add a comment |
They're not the same elements. The first one has uppercaseL
, the others have lowercasel
.
– Barmar
Aug 14 at 1:05
changed the typo
– Deke
Aug 14 at 1:08
Possible duplicate of Create an array with same element repeated multiple times
– Akrion
Oct 12 at 6:07
They're not the same elements. The first one has uppercase
L
, the others have lowercase l
.– Barmar
Aug 14 at 1:05
They're not the same elements. The first one has uppercase
L
, the others have lowercase l
.– Barmar
Aug 14 at 1:05
changed the typo
– Deke
Aug 14 at 1:08
changed the typo
– Deke
Aug 14 at 1:08
Possible duplicate of Create an array with same element repeated multiple times
– Akrion
Oct 12 at 6:07
Possible duplicate of Create an array with same element repeated multiple times
– Akrion
Oct 12 at 6:07
add a comment |
2 Answers
2
active
oldest
votes
up vote
15
down vote
accepted
For primitives, use .fill
:
var fruits = new Array(4).fill('Lemon');
console.log(fruits);
For non-primitives, don't use fill
, because then all elements in the array will reference the same object in memory, so mutations to one item in the array will affect every item in the array - instead, explicitly create the object on each iteration, which can be done with Array.from
:
var fruits = Array.from(
{ length: 4 },
() => ({ Lemon: 'Lemon' })
);
console.log(fruits);
add a comment |
up vote
3
down vote
Try using Array constructor:
let myArray = new Array(times).fill(elemnt)
See more here Array
1
You don't neednew
for arrays.
– Barmar
Aug 14 at 1:06
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
15
down vote
accepted
For primitives, use .fill
:
var fruits = new Array(4).fill('Lemon');
console.log(fruits);
For non-primitives, don't use fill
, because then all elements in the array will reference the same object in memory, so mutations to one item in the array will affect every item in the array - instead, explicitly create the object on each iteration, which can be done with Array.from
:
var fruits = Array.from(
{ length: 4 },
() => ({ Lemon: 'Lemon' })
);
console.log(fruits);
add a comment |
up vote
15
down vote
accepted
For primitives, use .fill
:
var fruits = new Array(4).fill('Lemon');
console.log(fruits);
For non-primitives, don't use fill
, because then all elements in the array will reference the same object in memory, so mutations to one item in the array will affect every item in the array - instead, explicitly create the object on each iteration, which can be done with Array.from
:
var fruits = Array.from(
{ length: 4 },
() => ({ Lemon: 'Lemon' })
);
console.log(fruits);
add a comment |
up vote
15
down vote
accepted
up vote
15
down vote
accepted
For primitives, use .fill
:
var fruits = new Array(4).fill('Lemon');
console.log(fruits);
For non-primitives, don't use fill
, because then all elements in the array will reference the same object in memory, so mutations to one item in the array will affect every item in the array - instead, explicitly create the object on each iteration, which can be done with Array.from
:
var fruits = Array.from(
{ length: 4 },
() => ({ Lemon: 'Lemon' })
);
console.log(fruits);
For primitives, use .fill
:
var fruits = new Array(4).fill('Lemon');
console.log(fruits);
For non-primitives, don't use fill
, because then all elements in the array will reference the same object in memory, so mutations to one item in the array will affect every item in the array - instead, explicitly create the object on each iteration, which can be done with Array.from
:
var fruits = Array.from(
{ length: 4 },
() => ({ Lemon: 'Lemon' })
);
console.log(fruits);
var fruits = new Array(4).fill('Lemon');
console.log(fruits);
var fruits = new Array(4).fill('Lemon');
console.log(fruits);
var fruits = Array.from(
{ length: 4 },
() => ({ Lemon: 'Lemon' })
);
console.log(fruits);
var fruits = Array.from(
{ length: 4 },
() => ({ Lemon: 'Lemon' })
);
console.log(fruits);
answered Aug 13 at 23:56
CertainPerformance
69.4k143453
69.4k143453
add a comment |
add a comment |
up vote
3
down vote
Try using Array constructor:
let myArray = new Array(times).fill(elemnt)
See more here Array
1
You don't neednew
for arrays.
– Barmar
Aug 14 at 1:06
add a comment |
up vote
3
down vote
Try using Array constructor:
let myArray = new Array(times).fill(elemnt)
See more here Array
1
You don't neednew
for arrays.
– Barmar
Aug 14 at 1:06
add a comment |
up vote
3
down vote
up vote
3
down vote
Try using Array constructor:
let myArray = new Array(times).fill(elemnt)
See more here Array
Try using Array constructor:
let myArray = new Array(times).fill(elemnt)
See more here Array
answered Aug 14 at 0:00
Facundo Petre
864
864
1
You don't neednew
for arrays.
– Barmar
Aug 14 at 1:06
add a comment |
1
You don't neednew
for arrays.
– Barmar
Aug 14 at 1:06
1
1
You don't need
new
for arrays.– Barmar
Aug 14 at 1:06
You don't need
new
for arrays.– Barmar
Aug 14 at 1:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51831998%2fhow-to-add-same-elements-to-javascript-array-n-times%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
They're not the same elements. The first one has uppercase
L
, the others have lowercasel
.– Barmar
Aug 14 at 1:05
changed the typo
– Deke
Aug 14 at 1:08
Possible duplicate of Create an array with same element repeated multiple times
– Akrion
Oct 12 at 6:07