II - objects

67.2% 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.


  • 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]


  • 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 the user and append it to the new array.