Array.map II - objects
Easy
19
1
73.3% Acceptance
Write a function called getAges(users)
, which takes in an array of objects users
as an argument. Each user object has three properties: name
, age
, and height
. Return a new array that contains only the ages of the users.
Instructions
- Return a new array
- If the input array is empty, then return an empty array
- Maintain the order of the elements as in the original array
Example test cases
const users = [ { name: 'John', age: 25, height: 175 }, { name: 'Mary', age: 30, height: 165 }, { name: 'Bob', age: 40, height: 180 }, { name: 'Jane', age: 20, height: 170 } ]; const ages = getAges(users); console.log(ages); // Output: [25, 30, 40, 20]
Hints
- You can create a new array, loop over the input array
users
and push the transformed element to the new array. - You can use the built-in method .
map()
method. - On each iteration of the loop, access the
age
property of theuser
and append it to the new array.