I have the current JSON object, named ‘data’. It has the following form:
{
"1": {
"A" {
"a": 2
}
}
}
I’m not sure of how I’m supposed to access the 2. I’ve tried everything. The errors keep coming back as undefined. All I need to do is access whatever value is attached to "a".
I’ve tried doing data["1"["A"[0]]], and almost all imaginable variations. Nothing.
I apologize, I’m new to the site and I’m not quite sure how to use the code editor. But yes, it is like that. And can you substitute “name1” for, let’s say, “1”? Then I would have to use bracket notation to access it. I’m not sure. Every time it comes out as undefined.
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.
As indicated, probably read up on how you access object properties: this isn’t a React thing, it’s a basic JS thing. Because this is pretty simple, as a tl/dr
myObj["1"].A.a
or
myObj["1"]["A"]["a"]
So myObj is your object. It has a key 1. You can only use dot notation if a key is a valid identifier: 1 is not that, so you need to use bracket notation to access it.
The value for the key 1 is an object. So to access the value for the key A in that object, you can use dot or bracket notation.
The value for key A is an object. So to access the value for key a in that object, you can use dot or bracket notation.