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

311
Views
How can I get a value from object in an array?

Given the following, how can I get the value for shape from the object to a 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

if you need the whole object you can use find or you can use reduce to get just what you need or you can get all the options with flatMap and than find the option you need

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

In your case it will be

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

But your structure is over complicated, if you have control over it then it should be something like

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!