Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda