How to copy an array n times using JavaScript

Let's assume that you have the following array:

javascript
let group = ['jeff', 'bill', 'elon', 'erik']

To copy the array n times at the end of it (concatenate n times):

javascript
let copy = group.join('__join__').concat('__join__').repeat(5).split('__join__')

The problem of the previous option is that you could have an array such as ['jeff', '__join__', 'bill'].

A better option:

javascript
let copy = 'n'.repeat(3).split('').reduce(acc => acc.concat(group), [])

Result:

javascript
// ['jeff', 'bill', 'elon', 'erik', 'jeff', 'bill', 'elon', 'erik', 'jeff', 'bill', 'elon', 'erik']

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.