Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
What would I code if I want to extract Alex's age from this array of objects?
var students = [ {
    name:"Mary",
    age: 10
  }, 
 
  {
    name:"Barbara",
    age:11
  }, 
 
  {
    name:"David",
    age:12
  },
  
   {
    name:"Alex",
    age:11
  } ];
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use for..of syntax for iterating over the array elements. If the element has .name that you need, extract the .age:

function getAge(arr, name) {
    for (let s of students) {
        if (s.name == name) return s.age;
    }
}

Then you can use this function like this:

alert(getAge(students, 'Alex'));
about 4 years ago · Juan Pablo Isaza Report

0

Use Array.find to get the item in the array whose name property is "Alex", then get the item's age property:

var students=[{name:"Mary",age:10},{name:"Barbara",age:11},{name:"David",age:12},{name:"Alex",age:11}];

let res = students.find(e => e.name == "Alex").age
console.log(res)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!