Get an array of property values from an object array
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
In JavaScript, the standard way to pull one property out of an array of objects is map. That solves the basic case in one line, but production code usually needs a bit more thought around missing fields, nested paths, filtering, and deduplication.
Use map for the basic case
If every object has the property you want, map is the right tool because it transforms each input element into exactly one output value.
That produces ["Ana", "Ben", "Cara"]. map is preferred over forEach here because you are creating a new array as the main result.
Decide what to do with missing properties
Real data is rarely perfect. If a property might be absent, decide whether you want:
- '
undefinedvalues preserved' - a fallback value substituted
- incomplete rows removed entirely
Here is a fallback approach:
If empty strings are not useful, filter after mapping:
Separating mapping from filtering keeps the intent clear. First extract values, then decide which values are acceptable.
Work with nested properties safely
API responses often contain nested objects, and direct property access can throw when a middle object is missing. Optional chaining makes this safer:
That pattern is better than wrapping extraction in try and catch. Missing data is usually a normal case, not an exceptional one.
Get unique property values
Sometimes the real goal is not "extract every value" but "extract the distinct values." Combine map with Set:
This is a good fit when you are building filter menus, chart legends, or quick summaries from object collections.
Write a reusable helper when the pattern repeats
If a codebase extracts fields often, a tiny helper can reduce repetition:
For nested or computed values, a getter function is even more flexible:
That approach scales better than inventing your own mini path language such as "customer.profile.city".
Choose the shape you actually need
Developers sometimes reach for a one-liner before deciding what the downstream code needs. That creates unnecessary cleanup later. Ask a few practical questions first:
- do you need one output value per input object
- should missing values stay in position or be removed
- are duplicates meaningful
- are you extracting a top-level field or computing a derived value
Once those answers are clear, the code usually becomes obvious.
Common Pitfalls
The most common mistake is using forEach and pushing into a separate array when map already expresses the transformation directly.
Another common problem is forgetting that map preserves length. If some objects do not have the property, you will still get an output entry for each one unless you filter afterward.
Nested access without optional chaining is another source of runtime errors, especially with API data.
Finally, do not over-generalize too early. A helper is useful when the pattern repeats, but a simple map call is usually clearer than a utility function for one-off code.
Summary
- Use
mapto extract one property value from each object in an array. - Decide explicitly how to handle missing values before writing the extraction.
- Use optional chaining for nested properties that may be absent.
- Combine
mapwithSetwhen you need unique values. - Prefer small helpers only when the extraction pattern repeats often.
Related reading
- Get an array of property values from an object array
- Get difference between two lists with Unique Entries
- Get difference between two lists with Unique Entries
- Get exception description and stack trace which caused an exception, all as a string
- get and set in TypeScript
- Get current scroll position of ScrollView in React Native
- Get file's size from bytes array without saving to disc
- Get first element from a dictionary

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.