make duplicate array in javascript code example
Example 1: javascript duplicate an array
let arr =["a","b","c"];
// ES6 way
const duplicate = [...arr];
// older method
const duplicate = Array.from(arr);
Example 2: function that duplicates data in array js
function doubleValues(array) {
var newArray = [];
array.forEach(function (el) { newArray.push(el, el); });
return newArray;
}
console.log(doubleValues([1,2,3]));
Example 3: javascript create array with repeated values
Array(5).fill(2)
//=> [2, 2, 2, 2, 2]