Skip to content

Instantly share code, notes, and snippets.

@ilio
Last active September 14, 2017 13:26
Show Gist options
  • Save ilio/a514e5b8902f4ffe7d3209ae57a7f521 to your computer and use it in GitHub Desktop.
Save ilio/a514e5b8902f4ffe7d3209ae57a7f521 to your computer and use it in GitHub Desktop.
const array = [{id:1}, {id:2}, {id:3}, {id:4}, {id:5}, {id:2}, {id:6}, {id:4}, {id:7}];
function unique(array, propertyName) {
return array.filter((e, i) => array.findIndex(a => a[propertyName] === e[propertyName]) === i);
}
console.log(unique(array, 'id'))
/*
output:
[{"id":1},{"id":2},{"id":3},{"id":4},{"id":5},{"id":6},{"id":7}]
*/
// perf: https://jsperf.com/compare-unique-array-by-property
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment