# React - How to access Object within Object?

**URL:** <https://forum.freecodecamp.org/t/react-how-to-access-object-within-object/360319>\
**Category:** General\
**Created:** [March 18, 2020, 9:45am UTC](https://forum.freecodecamp.org/t/react-how-to-access-object-within-object/360319 "2020-03-18T09:45:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![randomcoder](https://avatars.discourse-cdn.com/v4/letter/r/96bed5/32.png) [@randomcoder](https://forum.freecodecamp.org/u/randomcoder)\
**Post date:** [March 18, 2020, 9:45am UTC](https://forum.freecodecamp.org/t/react-how-to-access-object-within-object/360319/1 "2020-03-18T09:45:51Z")

</div>

I have the current JSON object, named ‘data’. It has the following form:

```auto
 {
     "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.

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [March 18, 2020, 9:51am UTC](https://forum.freecodecamp.org/t/react-how-to-access-object-within-object/360319/2 "2020-03-18T09:51:57Z")

</div>

Hello there.

Let us assume your object is declared like this:

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

```

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

```auto
myObject.name1.name2.name3

```

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

Hope this helps

---

<div class="post-metadata">

**Author:** ![randomcoder](https://avatars.discourse-cdn.com/v4/letter/r/96bed5/32.png) [@randomcoder](https://forum.freecodecamp.org/u/randomcoder)\
**Post date:** [March 18, 2020, 9:55am UTC](https://forum.freecodecamp.org/t/react-how-to-access-object-within-object/360319/3 "2020-03-18T09:55:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [March 18, 2020, 10:05am UTC](https://forum.freecodecamp.org/t/react-how-to-access-object-within-object/360319/4 "2020-03-18T10:05:31Z")

</div>

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

- [Build JavaScript Objects](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/build-javascript-objects)
- [Accessing Object Properties With Dot Notation](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-dot-notation)
- [Accessing Object Properties With Bracket Notation](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-bracket-notation)
- [Accessing Object Properties With Variables](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables)
- [Accessing Nested Objects](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/accessing-nested-objects)

---

<div class="post-metadata">

**Author:** ![KittyKora](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kittykora/32/124629_2.png) [@KittyKora](https://forum.freecodecamp.org/u/KittyKora)\
**Post date:** [March 18, 2020, 10:07am UTC](https://forum.freecodecamp.org/t/react-how-to-access-object-within-object/360319/5 "2020-03-18T10:07:32Z")

</div>

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

```auto
myObject.name1.name2.name3

```

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

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [March 18, 2020, 10:09am UTC](https://forum.freecodecamp.org/t/react-how-to-access-object-within-object/360319/6 "2020-03-18T10:09:51Z")

</div>

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](http://www.freecodecamp.com/forum/t/forum-code-formatting/25574) 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](https://sea3.discourse-cdn.com/freecodecamp/uploads/default/original/3X/4/b/4b9a26f0c243380dd84935f4e29b92e3ecee8d05.gif)

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [March 18, 2020, 11:54am UTC](https://forum.freecodecamp.org/t/react-how-to-access-object-within-object/360319/7 "2020-03-18T11:54:10Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 7:32pm UTC](https://forum.freecodecamp.org/t/react-how-to-access-object-within-object/360319/8 "2021-06-01T19:32:56Z")

</div>

Closed on June 1, 2021, 7:32pm UTC


