How to get distinct values from an array of objects in JavaScript?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Getting distinct values from an array of objects in JavaScript is really a question about what counts as "the same". Two objects can have the same business identity while still being different references in memory, so the usual solution is to derive a primitive key and deduplicate by that key explicitly.
Distinct Property Values with Set
If you only need the unique values of one property, use map plus Set.
This is the simplest case because Set works directly with primitive values such as strings and numbers.
Distinct Objects by One Key with Map
If you want distinct objects rather than distinct property values, use a key that defines uniqueness and store the objects in a Map.
Because later entries overwrite earlier ones in a Map, this version keeps the last object for each id. If you want the first one instead, guard the insert with map.has(key) before calling set.
Use a Set of Seen Keys for First-Wins Behavior
When keeping the first occurrence is important, a filter pass with a Set is often clearer.
This makes the policy explicit. You can read the code and know immediately that later duplicates are discarded.
Distinct by Multiple Properties
Sometimes uniqueness depends on more than one field. In that case, build a composite key.
This works well when the component fields are stable and easy to serialize. If the raw values can contain the delimiter, choose a safer encoding strategy.
Why Set Alone Does Not Deduplicate Objects
This is the point that trips people up most often:
Both objects remain because JavaScript compares object references in a Set, not structural content. Even though a and b look equivalent, they are different objects in memory.
That is why object deduplication almost always starts by converting the business identity into a primitive key.
Avoid JSON.stringify as the Default
You may see examples like this:
This can work for small, controlled data, but it is a shortcut with limitations. Property order matters, unsupported values behave awkwardly, and the rule for equality becomes harder to reason about than an explicit key.
Use it only when the data is truly JSON-shaped and structural equality is exactly what you want.
Common Pitfalls
- Using
new Set(arrayOfObjects)and expecting structurally equal objects to collapse automatically. - Forgetting to choose whether the first duplicate or last duplicate should win.
- Building composite keys without thinking about missing fields or delimiter collisions.
- Using
JSON.stringifyas a universal deduplication trick even when the object shape is unstable. - Mixing up distinct property values with distinct whole objects.
Summary
- Define uniqueness first, then choose the deduplication technique.
- Use
Setdirectly for primitive property values. - Use
Mapor aSetof derived keys for arrays of objects. - Build composite keys when uniqueness depends on more than one field.
- Treat
JSON.stringifyas a limited shortcut, not the default design.

