Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat an array with multiple elements multiple times in JavaScript

In JavaScript, how can I repeat an array which contains multiple elements, in a concise manner?

In Ruby, you could do

irb(main):001:0> ["a", "b", "c"] * 3 => ["a", "b", "c", "a", "b", "c", "a", "b", "c"] 

I looked up the lodash library, and didn't find anything that was directly applicable. Feature request: repeat arrays. is a feature request for adding it to lodash, and the best workaround given there is

const arrayToRepeat = [1, 2, 3]; const numberOfRepeats = 3; const repeatedArray = _.flatten(_.times(numberOfRepeats, _.constant(arrayToRepeat))); 

The questions Most efficient way to create a zero filled JavaScript array? and Create an array with same element repeated multiple times focus on repeating just a single element multiple times, whereas I want to repeat an array which has multiple elements.

Using reasonably well-maintained libraries is acceptable.

like image 248
Andrew Grimm Avatar asked Jun 04 '18 00:06

Andrew Grimm


People also ask

How do you repeat an array multiple times?

For Loop and Array. We can use the for loop to run the push method to add an item to an array multiple times. We create the fillArray function that has the value and len parameters. value is the value we want to fill in the array. len is the length of the returned array.

How do you repeat values in an array?

The repeat() function is used to repeat elements of an array. Input array. The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis.

Can array have duplicate values JavaScript?

To check if an array contains duplicates: Pass the array to the Set constructor and access the size property on the Set . Compare the size of the Set to the array's length. If the Set contains as many values as the array, then the array doesn't contain duplicates.

How do you repeat a value in JavaScript?

repeat() is an inbuilt function in JavaScript which is used to build a new string containing a specified number of copies of the string on which this function has been called. Syntax: string. repeat(count);


2 Answers

No need for any library, you can use Array.from to create an array of arrays you want repeated, and then flatten using [].concat and spread:

const makeRepeated = (arr, repeats) =>    [].concat(...Array.from({ length: repeats }, () => arr));      console.log(makeRepeated([1, 2, 3], 2));

On newer browsers, you can use Array.prototype.flat instead of [].concat(...:

const makeRepeated = (arr, repeats) =>    Array.from({ length: repeats }, () => arr).flat();      console.log(makeRepeated([1, 2, 3], 2));
like image 198
CertainPerformance Avatar answered Sep 24 '22 18:09

CertainPerformance


You can use the Array constructor along with its fill method to fill it a number of times of the array you want to repeat, then concat them (the subarrays) into a single array:

const repeatedArray = [].concat(...Array(num).fill(arr)); 

Note: On older browsers (pre-ES6), you can use Function#apply to mimic the rest syntax above (concat will be called with each of the sub arrays passed to it as argument):

var repeatedArray = [].concat.apply([], Array(num).fill(arr)); 

Example:

const arrayToRepeat = [1, 2, 3];  const numberOfRepeats = 3;    const repeatedArray = [].concat(...Array(numberOfRepeats).fill(arrayToRepeat));    console.log(repeatedArray);
like image 23
ibrahim mahrir Avatar answered Sep 20 '22 18:09

ibrahim mahrir