Facebook Pixel
map in JavaScript: Examples and Use Cases

How do you extract a property from an array of objects with map in JavaScript?

The answer

Use map with a callback that returns the property: users.map(u => u.name). This returns a new array of just the names.

More FAQs in map in JavaScript: Examples and Use Cases

It calls a callback on each element of the array and returns a new array with the results. The new array has the same length as the original. The original array is not mutated.

Use spread to create a new object with the added property: users.map((u, i) => ({ ...u, id: i + 1 })). Do not mutate the original object.

Because the arrow function has a block body (braces) with no return statement. Block bodies need an explicit return. Use an expression body (n => n * 2) or add return (n => { return n * 2; }).

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.