repeat a var in array n times javascript code example
Example 1: duplicate an array in javascript n times
const makeRepeated = (arr, repeats) =>
Array.from({ length: repeats }, () => arr).flat();
console.log(makeRepeated([1, 2, 3], 2));
Example 2: repeat an element in array in js
Array(5).fill(2)
//=> [2, 2, 2, 2, 2]