Skip to content

Instantly share code, notes, and snippets.

@tuxsudo
Last active December 18, 2015 01:09
Show Gist options
  • Save tuxsudo/ae9426efe2884e8d4846 to your computer and use it in GitHub Desktop.
Save tuxsudo/ae9426efe2884e8d4846 to your computer and use it in GitHub Desktop.
export function dedupeByMatchingObjectsValues( keys , arrayOfObjects ) {
return arrayOfObjects.filter( (item, i, inArray) => (
inArray.find( target => (
[].concat(keys).reduce( (bool, key) => bool && item[ key ]===target[ key ], true)
))===item
));
}
import {dedupeByMatchingObjectsValues} from './dedupeByMatchingObjectsValues.js';
let arrayWithDuplicates = [{a: 1, b:2}, {a: 2}, {a: 1, b:2}, {a: 1}, {a: 3}, {a: 1}, {a: 1}, {a: 2}, {a: 8}];
let unique = dedupeByMatchingObjectsValues(['a', 'b'], arrayWithDuplicates);
console.log(unique);
// [{"a":1,"b":2},{"a":2},{"a":1},{"a":3},{"a":8}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment