This function repeats the elements of an array a given number of times.
JavaScript:
let repeat = (arr, times) => Array(times).fill([...arr]).flat();
// Example
console.log(repeat([1, 2, 3], 2)); // [1, 2, 3, 1, 2, 3]
console.log(repeat(['a', 'b', 'c'], 3)); // ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
TypeScript:
let repeat = (arr: T[], times: number): T[] => Array(times).fill([...arr]).flat();
// Example
console.log(repeat([1, 2, 3], 2)); // [1, 2, 3, 1, 2, 3]
console.log(repeat(['a', 'b', 'c'], 3)); // ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']