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?