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

163
Views
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
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!