Skip to content

Instantly share code, notes, and snippets.

@tasandberg
Created October 24, 2017 05:34
Show Gist options
  • Save tasandberg/1bc109993ddd620f10bb433afff53363 to your computer and use it in GitHub Desktop.
Save tasandberg/1bc109993ddd620f10bb433afff53363 to your computer and use it in GitHub Desktop.
Remove duplicates from array of Mongo ObjectIDs
/**
* Given many arrays of ObjectIDs, return a unique list
*
* @params objectIDs {Array<ObjectID>}
* @returns
*/
module.exports = function dedupeIDs(objectIDs) {
const ids = {}
objectIDs.forEach(_id => (ids[_id.toString()] = _id))
return Object.values(ids)
}
@ryanosten
Copy link

This really helped me once I figured out that I wasnt working with strings. Couldnt figure out why traditional methods for removing duplicates in array weren't working.

@fonzane
Copy link

fonzane commented Feb 11, 2022

This doesn't work for me.
MongoDB ObjectIDs Duplicates
The first line is the log of the ObjectIDs of a property of a document that I get from the DB.
The second line is the result of a concat method in which I merge the array from line 1 with an array with another ObjectID.
The third line is the result of your function, which seemingly gives me the wrong result. 🙁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment