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.
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.
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.
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.
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);
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));
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With