javascript filter array remove duplicates
//ES6
let uniqueArray = [...new Set(arrayWithDuplicates)];
//Alternative
function removeArrayDuplicates(arrayWithDuplicates) {
let seen = {};
let uniqueArray = [];
let len = arrayWithDuplicates.length;
let j = 0;
for(let i = 0; i < len; i++) {
let item = arrayWithDuplicates[i];
if(seen[item] !== 1) {
seen[item] = 1;
uniqueArray[j++] = item;
}
}
return uniqueArray;
}// Using the Set constructor and the spread syntax:
arrayWithOnlyUniques = [...new Set(arrayWithDuplicates)]
Also in JavaScript:
- create many items from an array to canvas
- cancel settimeout
- js object destructuring with defaults
- npm windows shocut
- Angular comment
- how to send json data in fetch request body
- js use await in synchronous method
- prevent default on enter press
- close exit app react native
- javascript object string property
- jquery select input with empty value
- expressjs create encrypted password
- jquery autoSuggestList
- selection sort javascript
- javascript modify url without reloading page
- javascript hide element by class
- get user time using timezone javascript
- how to print to screen in javascript
- check unique object in array javascript site:stackoverflow.com
- what are callbacks
- javascript check if character exists in string
- --resolveJsonModule
- check if outside the div is clicked
- cubic root javascript