Avatar ·

How to extract values of a specific property from an array of objects into a new array?

Hello everyone! I just started learning JavaScript and came across this task. I have an array of objects, each object represents, say, a user with fields name, age, and email. I need to get an array that contains only the values of the name property from each object. That is, from this array:

\n
const users = [\n  { name: 'Anna', age: 25, email: 'anna@example.com' },\n  { name: 'Ivan', age: 30, email: 'ivan@example.com' },\n  { name: 'Maria', age: 22, email: 'maria@example.com' }\n];
\n

I want to get:

\n
['Anna', 'Ivan', 'Maria']
\n

I tried doing this with a for loop, but it turns out cumbersome. I've heard there are more elegant ways in modern Ja