Created
June 25, 2020 11:29
-
-
Save Kevinwkd/0f14c74a8e1a0829f1270f3d748a66a1 to your computer and use it in GitHub Desktop.
This ts code snippet is for removing the duplication in an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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