Skip to content

Instantly share code, notes, and snippets.

@Kevinwkd
Created June 25, 2020 11:29
Show Gist options
  • Save Kevinwkd/0f14c74a8e1a0829f1270f3d748a66a1 to your computer and use it in GitHub Desktop.
Save Kevinwkd/0f14c74a8e1a0829f1270f3d748a66a1 to your computer and use it in GitHub Desktop.
This ts code snippet is for removing the duplication in an array
import { ICompareFunc } from '../interfaces/Util';
export const removeNonPrimitiveDuplicate = (arr: any[], compareFunc: ICompareFunc) => {
return arr.reduce((unique, item) => {
let duplicated: boolean = false;
for(const key in unique) {
if (compareFunc(unique[key], item)) {
duplicated = true;
break;
}
}
if(!duplicated) {
unique.push(item);
}
duplicated = false;
return unique;
}, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment