Created
March 13, 2018 15:21
-
-
Save aaronbeall/009888ac96f0de236140399c8f993294 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function uniqueArray<T>(array: T[], toUniqueValue?: (current: T) => any): T[] { | |
const values = toUniqueValue ? array.map(toUniqueValue) : array; | |
return array.filter((current, index) => values.indexOf(values[index]) == index); | |
} | |
// Example | |
const array = [{ id: 0, k: "a" }, { id: 1, k: "b" }, { id: 2, k: "a" }, { id: 3, k: "c" }]; | |
const result = uniqueArray(array, item => item.k); | |
expect(result).toEqual([{ id: 0, k: "a" }, { id: 1, k: "b" }, { id: 3, k: "c" }]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment