23
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)
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
Deke
  • 4,451
  • 4
  • 44
  • 65
  • They're not the same elements. The first one has uppercase `L`, the others have lowercase `l`. – Barmar Aug 14 '18 at 01:05
  • changed the typo – Deke Aug 14 '18 at 01:08
  • Possible duplicate of [Create an array with same element repeated multiple times](https://stackoverflow.com/questions/12503146/create-an-array-with-same-element-repeated-multiple-times) – Akrion Oct 12 '18 at 06:07

4 Answers4

44

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.

const fruits = new Array(4).fill({ Lemon: 'Lemon' });

fruits[0].Apple = 'Apple';
console.log(JSON.stringify(fruits));


// The above doesn't work for the same reason that the below doesn't work:
// there's only a single object in memory

const obj = { Lemon: 'Lemon' };

const fruits2 = [];
fruits2.push(obj);
fruits2.push(obj);
fruits2.push(obj);
fruits2.push(obj);

fruits2[0].Apple = 'Apple';
console.log(JSON.stringify(fruits2));

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 an example of how to create a 2D array this way:

var fruits = Array.from(
  { length: 2 }, // outer array length
  () => Array.from(
    { length: 3 }, // inner array length
    () => ({ Lemon: 'Lemon' })
  )
);
console.log(fruits);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Apparently here's *["Why never use new Array in Javascript"](https://coderwall.com/p/h4xm0w/why-never-use-new-array-in-javascript)* – ashleedawg Apr 13 '21 at 14:54
  • 1
    Sure, don't use it *wrong*. There are lots of ways to use lots of things wrong, unfortunately, and neither `new Array` nor `Array.from` are exceptions. `new Array` is fine as long as you fill the array afterwards. – CertainPerformance Apr 13 '21 at 17:00
  • check out this [question in stack overflow](https://stackoverflow.com/a/76340086/6908282) which shows that `Array.fill` is better in performance compared to for loop – Gangula May 26 '23 at 10:55
3

Try using Array constructor:

let myArray = Array(times).fill(elemnt)

See more here Array

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Facundo Petre
  • 281
  • 2
  • 5
2

You shouldn't use the array constructor, use [] instead.

const myArray = [];   // declare array

myArray.length = 5; // set array size
myArray.fill('foo'); // fill array with any value

console.log(myArray); // log it to the console
Mystical
  • 2,505
  • 2
  • 24
  • 43
1
   const item = 'lemon'
   const arr = Array.from({length: 10}, () => item)

const item = 'lemon'
const arr = Array.from({length: 10}, () => item)
console.log('arr', arr)
Anna Vlasenko
  • 916
  • 5
  • 8