How to get distinct values from an array of objects in JavaScript?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
Related reading
- How to get first element in a list of tuples?
- How to get first N number of elements from an array
- How to get first object out from ListObject using Linq
- How to get indices of a sorted array in Python
- How to get file descriptor in node.js if you open file using openSync
- How to get GET (query string) variables in Express.js on Node.js?
- How to get IntPtr from byte in C
- How to get largest number of consecutive integers in a substantially large array (spread across multiple machines)

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.