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

164
Vistas
Javascript Object property accessor string with array position square brackets

For Example I have this Object structure.

let data = {
  id: 2, 
  content: [
    {foo: "bar1"},
    {foo: "bar2"},
  ]
};

And the only thing I know is this String:

content[0].foo

How can I access the property with this square brackets in the string?

I know that you can access objects like this:

data['content'] // (2) [{…}, {…}]
data['content'][0] // {foo: 'bar1'}

I tried to use something like this data['content[0]'] but this is undefined because here js tries to get the key that has square brackets in it.

I have more complex data structures like this in my project: data.content[0].carousel.images[3].id.

What is the best possible way to access the property only knowing the string to the correct property containing square brackets for the array positions?

about 4 years ago · Juan Pablo Isaza
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