JavaScript
Array Methods
Object Arrays
Data Manipulation
Coding Tutorial

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.

Practice algorithms

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.

javascript
1const users = [
2  { id: 1, name: "Ana", role: "admin" },
3  { id: 2, name: "Ben", role: "editor" },
4  { id: 3, name: "Cara", role: "viewer" }
5];
6
7const names = users.map(user => user.name);
8console.log(names);

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:

  • 'undefined values preserved'
  • a fallback value substituted
  • incomplete rows removed entirely

Here is a fallback approach:

javascript
1const rows = [
2  { id: 1, email: "[email protected]" },
3  { id: 2 },
4  { id: 3, email: "[email protected]" }
5];
6
7const emails = rows.map(row => row.email ?? "");
8console.log(emails);

If empty strings are not useful, filter after mapping:

javascript
1const validEmails = rows
2  .map(row => row.email)
3  .filter(email => typeof email === "string" && email.length > 0);
4
5console.log(validEmails);

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:

javascript
1const orders = [
2  { id: 10, customer: { profile: { city: "Toronto" } } },
3  { id: 11, customer: null },
4  { id: 12, customer: { profile: { city: "Paris" } } }
5];
6
7const cities = orders.map(order => order.customer?.profile?.city ?? "unknown");
8console.log(cities);

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:

javascript
1const products = [
2  { sku: "A1", category: "books" },
3  { sku: "B2", category: "books" },
4  { sku: "C3", category: "games" }
5];
6
7const categories = [...new Set(products.map(product => product.category))];
8console.log(categories);

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:

javascript
1function pluck(array, key, fallback = undefined) {
2  return array.map(item => item?.[key] ?? fallback);
3}
4
5console.log(pluck(users, "role", "guest"));

For nested or computed values, a getter function is even more flexible:

javascript
1function pluckBy(array, getter, fallback = undefined) {
2  return array.map(item => getter(item) ?? fallback);
3}
4
5const citiesAgain = pluckBy(orders, order => order.customer?.profile?.city, "unknown");
6console.log(citiesAgain);

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 map to 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 map with Set when you need unique values.
  • Prefer small helpers only when the extraction pattern repeats often.

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.