Skip to content

Instantly share code, notes, and snippets.

@thomasmichaelwallace
Created August 1, 2017 16:20
Show Gist options
  • Save thomasmichaelwallace/e3b92682485f394459399f531fb13a53 to your computer and use it in GitHub Desktop.
Save thomasmichaelwallace/e3b92682485f394459399f531fb13a53 to your computer and use it in GitHub Desktop.
Getting unique values using sets in ES6.
// 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