Last active
December 18, 2015 01:09
-
-
Save tuxsudo/ae9426efe2884e8d4846 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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 | |
| )); | |
| } |
This file contains hidden or 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
| 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