# javascript - Javascript - 如何"重复"数组的内容

``````
var array_1 = [\'c\',\'b\',\'a\'];

``````

``````
[\'c\',\'c\',\'b\',\'b\',\'a\',\'a\'];

``````

``````
[\'c\',\'c\',\'c\',\'b\',\'b\',\'b\',\'a\',\'a\',\'a\'];

``````

``````
function duplicate(arr, times) {

return arr.flatMap(n => Array(times).fill(n))

}

console.log(JSON.stringify(duplicate([1,2,3], 2)))

console.log(JSON.stringify(duplicate([1,2,3], 3)))
``````

``````
var arr1 = [\'c\', \'b\', \'a\'];

var arr2 = arr1.map(e => ([e, e, e])).flat();

console.log(arr2);
``````

``````
const ar1 = [\'a\', \'b\', \'c\']

const ar2 = ar1.reduce((ac, cv) => [...ac, cv, cv, cv], [])

``````

``````
const myArray = [\'c\', \'b\', \'a\'];

const resultArr1 = duplicate(myArray, 2);

console.log(resultArr1);

const resultArr2 = duplicate(myArray, 3);

console.log(resultArr2);

function duplicate(arr, times) {

let duplicateArray = [];

arr.forEach(item => {

for(i=0; i < times; i++) {

duplicateArray.push(item);

}

});

return duplicateArray;

}
``````

``````
public static char[] duplicateAnarray(char arr[], int duplicacyCount) {

char arrnew[] = new char[((arr.length) * duplicacyCount)];

char temp;

int p = 0;

for (int k = 0; k < arr.length; k++) {

for (int i = p; i < arrnew.length; i++) {

for (int j = 0; j < duplicacyCount; j++) {

arrnew[i + j] = arr[k];

}

p = i + duplicacyCount;

break;

}

}

return arrnew;

}

``````
``````
var array_1 = [\'a\',\'b\',\'c\']

/* if array has numbers array_1 = [1,2,3] both case are handled and you can change duplicacy number for elements.*/

getResult = (array_1,elemDuplicacyNumber)=>{

return elemDuplicacyNumber <= 0 ? array_1 : array_1.reduce((curr, acc) =>{

let arr1 = Array(elemDuplicacyNumber).fill(curr).join(\',\').split(\',\');

let arr2 = Array(elemDuplicacyNumber).fill(acc).join(\',\').split(\',\');

arr1 = isNaN(curr) ? arr1 : arr1.map(Number);

arr2 = isNaN(acc) ? arr2 : arr2.map(Number);

return (Array.isArray(curr)?curr : arr1 ).concat(arr2)

})

}

console.log( getResult(array_1,5) )

``````