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

196
Views
Find a single value in array of objects in JavaScript

I'm a noob when it comes to JS and am trying to figure out how to return a single value instead of an object when using the .find() function.

Example:

var obj = [
    { name: 'Max', age: 23 },
    { name: 'John', age: 20 },
    { name: 'Caley', age: 18 }
];
 
var found = obj.find(e => e.name === 'John');
console.log(found);

This will output { name: 'John', age: 20 }

I want it to output John's age (ie: 20) as a string.

What am I missing?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Simple add ?.age to your found variable.

var obj = [
    { name: 'Max', age: 23 },
    { name: 'John', age: 20 },
    { name: 'Caley', age: 18 }
];
 
var found = obj.find(e => e.name === 'John')?.age; // Using ?. incase value isnt found.
console.log(found);

You also mention you need it as a string, you can do achieve by wrapping the found var in the String function like so...

var obj = [
    { name: 'Max', age: 23 },
    { name: 'John', age: 20 },
    { name: 'Caley', age: 18 }
];
 
var found = String(obj.find(e => e.name === 'John')?.age);
console.log(found);
about 4 years ago · Juan Pablo Isaza Report

0

.find function return you an object so you can just do something like this

var age = obj.find(e => e.name === "John").age
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!