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

154
Vistas
How to access object's name property

Consider a const that holds an object with multiple levels like this:

const test = {
  One: {
    a: 220,
    b: ['Chipotle', 'Subway', 'Habit Burger'],
  },
  Two: {
    a: 110,
    b: ['Chipotle'],
  },
  Three: {
    a: 200,
    b: ['Subway', 'Jack In The Box'],
  },
  Four: {
    a: 460,
    b: ['Chipotle', 'Subway', 'Habit Burger', 'Jack In The Box'],
  },
};

I know that, for example, if I want to access the first object's a value, I can say test.One.a, but how do I programmatically access the value of test.One, test.Two, etc. without hardcoding it?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use the bracket notation for accessing properties:

test["One"]["a"];
test["Two"]["b"];

These work with strings, so you can use variables:

const test = {
  One: {
    a: 220,
    b: ['Chipotle', 'Subway', 'Habit Burger'],
  },
  Two: {
    a: 110,
    b: ['Chipotle'],
  },
  Three: {
    a: 200,
    b: ['Subway', 'Jack In The Box'],
  },
  Four: {
    a: 460,
    b: ['Chipotle', 'Subway', 'Habit Burger', 'Jack In The Box'],
  },
};

const first = Object.keys(test)[Math.floor(Math.random() * 4)];
const second = ["a", "b"][Math.floor(Math.random() * 2)];

console.log(first, second, test[first][second])

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