Skip to content

Instantly share code, notes, and snippets.

@ihaveaproblem
Forked from thomasmichaelwallace/unique.js
Created February 21, 2019 10:22
Show Gist options
  • Save ihaveaproblem/f2c009331e03bf6fd0da80c8fe2c7635 to your computer and use it in GitHub Desktop.
Save ihaveaproblem/f2c009331e03bf6fd0da80c8fe2c7635 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