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

318
Views
¿Cómo puedo obtener un valor de un objeto en una matriz?

Teniendo en cuenta lo siguiente, ¿cómo puedo obtener el valor de la shape del objeto a una variable?

 var data = [ { "Category": "Color", "Options": [ { "Key": "color", "Value": "red" } ] }, { "Category": "Shape", "Options": [ { "Key": "shape", "Value": "circle" } ] } ]; var shape = // want this to be 'circle'
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

si necesita todo el objeto, puede usar find o puede usar reduce para obtener justo lo que necesita o puede obtener todas las opciones con flatMap y luego encontrar la opción que necesita

 var data = [ { "Category": "Color", "Options": [ { "Key": "color", "Value": "red" } ] }, { "Category": "Shape", "Options": [ { "Key": "shape", "Value": "circle" } ] } ]; const shape = data.find(d => d.Category === 'Shape') const shapeOption = data.reduce((res, d) => { if(d.Category === 'Shape'){ return d.Options.find(o => o.Key === 'shape').Value } return res }, null) const shapeWithTransformations = data.flatMap(d => d.Options).find((o) => o.Key === 'shape').Value console.log(shape) console.log(shapeOption) console.log(shapeWithTransformations)

about 4 years ago · Juan Pablo Isaza Report

0

En tu caso será

 var data = [ { "Category": "Color", "Options": [ { "Key": "color", "Value": "red" } ] }, { "Category": "Shape", "Options": [ { "Key": "shape", "Value": "circle" } ] } ]; const shape = data[1]['Options'][0]['Value']; console.log(shape);

Pero su estructura es demasiado complicada, si tiene control sobre ella, entonces debería ser algo como

 const data = { 'Color': { 'Key': 'color', 'Value': 'red' }, 'Shape': { 'Key': 'shape', 'Value': 'circle' } }; const shape = data['Shape']['Value']; console.log(shape);

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!