how to duplicate an array in js code example
Example 1: js array clone
let clone = [...myArray]
Example 2: javascript duplicate an array
let arr =["a","b","c"];
// ES6 way
const duplicate = [...arr];
// older method
const duplicate = Array.from(arr);
Example 3: javascript copy array
var numbers = [1,2,3,4,5];
var newNumbers = Object.assign([], numbers);
Example 4: javascript find duplicate in array
// JavaScript - finds if there is duplicate in an array.
// Returns True or False.
const isThereADuplicate = function(arrayOfNumbers) {
// Create an empty associative array or hash.
// This is preferred,
let counts = {};
// // but this also works. Comment in below and comment out above if you want to try.
// let counts = [];
for(var i = 0; i <= arrayOfNumbers.length; i++) {
// As the arrayOfNumbers is being iterated through,
// the counts hash is being populated.
// Each value in the array becomes a key in the hash.
// The value assignment of 1, is there to complete the hash structure.
// Once the key exists, meaning there is a duplicate, return true.
// If there are no duplicates, the if block completes and returns false.
if(counts[arrayOfNumbers[i]] === undefined) {
counts[arrayOfNumbers[i]] = 1;
} else {
return true;
}
}
return false;
}
Example 5: duplicate numbers in an array javascript
[1, 1, 2, 2, 3].filter((element, index, array) => array.indexOf(element) !== index) // [1, 2]