duplicate array in javascript code example
Example 1: javascript copy an array
let arr =["a","b","c"];
const copy = [...arr];
const copy = Array.from(arr);
Example 2: how to get duplicate values from array in javascript
var array = [1, 2, 2, 3, 3, 4, 5, 6, 2, 3, 7, 8, 5, 22, 1, 2, 511, 12, 50, 22];
console.log([...new Set(
array.filter((value, index, self) => self.indexOf(value) !== index))]
);
Example 3: js array clone
let clone = [...myArray]
Example 4: javascript duplicate an array
let arr =["a","b","c"];
const duplicate = [...arr];
const duplicate = Array.from(arr);
Example 5: javascript copy array
var countries = [
{name: 'USA', population: '300M'},
{name: 'China', population: '1.6B'}
];
var newCountries = JSON.parse(JSON.stringify(countries));