Created
November 4, 2020 19:57
-
-
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
This file contains 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
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