JavaScript
Array Deduplication
Deduplicate an array using Set.
By EZ4Code Team
arraysetdedup
Code
function unique(arr) {
return [...new Set(arr)];
}
// Deduplicate object array by specified field
function uniqueBy(arr, key) {
const seen = new Set();
return arr.filter(item => {
const val = item[key];
if (seen.has(val)) return false;
seen.add(val);
return true;
});
}Explanation
Leverages Set's element uniqueness for fast deduplication; object arrays can be deduplicated by a specified field.
More JavaScript Snippets
Array Map Filter Reduce
Chain map, filter, reduce, find, some, and every on arrays.
Deep Clone
Deep clone objects, supporting common data types.
Debounce Function
Wait a period of time after an event triggers before executing; reset the timer if triggered again during the wait.
Throttle Function
Limit a function to execute at most once within a time interval.
Promise.all Concurrency Control
A Promise executor with concurrency limit.
async/await Error Handling
Wrap async functions to uniformly catch exceptions.