Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DoctorDerek/4fcdc502f0f37ec363782d5e9d0551ba to your computer and use it in GitHub Desktop.
Save DoctorDerek/4fcdc502f0f37ec363782d5e9d0551ba to your computer and use it in GitHub Desktop.
Easy One-Liner to Use Set to Find Unique Objects within an Array of Objects by the Objects' Contents
const uniqueObjectsOneLiner = [
...new Set(objectsArray.map((o) => JSON.stringify(o))),
].map((string) => JSON.parse(string))
console.log(`${uniqueObjectsOneLiner.length} objects`)
// Output: 2 objects
console.log(...uniqueObjectsOneLiner)
// [ { id: 1, emoji: "🎸" }, { id: 2, emoji: "🎷" } ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment