Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

313
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda