React - How to access Object within Object?

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.

Hello there.

Let us assume your object is declared like this:

const myObject = {
  "name1": {
    "name2": {
      "name3": 2
    }
  }
}

To access the 2, you would do something like this:

myObject.name1.name2.name3

However, if one of the keys is a number, you will have to use bracket notation.

Hope this helps

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.

Somebody please help me

I will suggest you do/review these challenges in the JavaScript curriculum:

If you where to subsitute the name1 for 1 it be a number. It could still works

As above post
it would be something like this

myObject.name1.name2.name3

I recomend using the link bellow to see what your code is doing
http://pythontutor.com/visualize.html#mode=display

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.

Note: Backticks are not single quotes.

markdown_Forums

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.

myObj
// { "1": { A: { a: 2 } } }
myObj["1"]
// { A: { a: 2 } }
myObj["1"]["A"]
// { a: 2 }
myObj["1"]["A"]["a"]
// 2