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

188
Views
Encuentre un valor único en una matriz de objetos en JavaScript

Soy un novato cuando se trata de JS y estoy tratando de descubrir cómo devolver un valor único en lugar de un objeto cuando uso la función .find().

Ejemplo:

 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);

Esto generará { name: 'John', age: 20 }

Quiero que muestre la edad de John (es decir, 20 ) como una cadena.

¿Qué me estoy perdiendo?

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

0

Simplemente agregue ?.age a su variable found .

 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);

También menciona que lo necesita como una cadena, puede lograrlo envolviendo la var found en la función String así ...

 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

La función .find le devuelve un objeto para que pueda hacer algo como esto

 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!