-
-
Save ihaveaproblem/f2c009331e03bf6fd0da80c8fe2c7635 to your computer and use it in GitHub Desktop.
Getting unique values using sets in ES6.
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
// with arrays | |
const dupArr = [1, 1, 2, 3, 1]; | |
const uniArr = [...(new Set(dupArr))]; | |
// [1, 2, 3] | |
// with objects on a key. | |
const dupObj = [{ id: 1, value: 'a' }, { id: 2, value: 'b' }, { id: 1, value: 'c' }]; | |
const uniKeys = [...(new Set(dupObj.map(({ id }) => id)))]; | |
// [ '1', '2' ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment