# Access array inside nested object \[solved\]

**URL:** <https://forum.freecodecamp.org/t/access-array-inside-nested-object-solved/105355>\
**Category:** General\
**Created:** [April 12, 2017, 4:03pm UTC](https://forum.freecodecamp.org/t/access-array-inside-nested-object-solved/105355 "2017-04-12T16:03:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eyhln](https://avatars.discourse-cdn.com/v4/letter/e/8edcca/32.png) [@eyhln](https://forum.freecodecamp.org/u/eyhln)\
**Post date:** [April 12, 2017, 4:03pm UTC](https://forum.freecodecamp.org/t/access-array-inside-nested-object-solved/105355/1 "2017-04-12T16:03:48Z")

</div>

I am working on the weather project and I am trying to access elements of an array stored inside a nested object in order to dynamically set the background based on the weather conditions. My code:

```
function selectBackgroundPhoto(code) {
  var photos = {
    fog: {codes: [1030, 1135, 1147], url: "https://s24.postimg.org/owuib3pn5/mist.jpg"},
    partlycloudy: {codes: [1003], url: "https://s24.postimg.org/6rhjx1q4x/Cloudy_sky_Turkey.jpg"},
    cloudy: {codes: [1006, 1009], url: "https://s24.postimg.org/ebamz3jbl/nubes-grises.jpg"},
    sunny: {codes: [1000], url: "https://s24.postimg.org/tmkfzpgnl/sky-sunny-clouds-cloudy.jpg"},
    rain: {codes: [1063, 1087, 1150, 1153, 1180, 1183, 1186, 1189, 1192, 1195, 1240, 1243, 1246, 1273, 1276], url: "https://s24.postimg.org/daae9z2c1/rain-455120_960_720.jpg"},
    sleet: {codes: [1069, 1072, 1168, 1171, 1198, 1201, 1204, 1207, 1237, 1249, 1252, 1261, 1264], url: "https://s24.postimg.org/m7v47bus1/sleet.jpg"}}
  
  for (var prop in photos) {
    if (photos.hasOwnProperty(prop)) {
      if (prop.codes.find(code) !== undefined) {
        return prop.url;
      }
    }
  }
}

```

I have been investigating dot vs. bracket notation for accessing properties, and with my current understanding I think it is set up correctly on that front. I receive a `TypeError: prop.codes is not defined` when I run the code. What is incorrect?

---

<div class="post-metadata">

**Author:** ![mblood](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@mblood](https://forum.freecodecamp.org/u/mblood)\
**Post date:** [April 12, 2017, 4:14pm UTC](https://forum.freecodecamp.org/t/access-array-inside-nested-object-solved/105355/2 "2017-04-12T16:14:47Z")

</div>

Try changing `prop.codes.find(code)` to `photos[prop].codes.find(code)` in the for loop. You’ll also need to return `photos[prop].url` instead of `prop.url`.

When you iterate over an object’s properties, you’re only getting the property name, not the actual object property. You need to actually access the property, in this case with `photos[prop]`.

---

<div class="post-metadata">

**Author:** ![eyhln](https://avatars.discourse-cdn.com/v4/letter/e/8edcca/32.png) [@eyhln](https://forum.freecodecamp.org/u/eyhln)\
**Post date:** [April 12, 2017, 4:24pm UTC](https://forum.freecodecamp.org/t/access-array-inside-nested-object-solved/105355/3 "2017-04-12T16:24:33Z")

</div>

Ah, okay. I find that my confusion about correctness is tied in with my misuse of the Array.prototype.find() function. Thanks for helping me fix my access notation!

---

<div class="post-metadata">

**Author:** ![honmanyau](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/honmanyau/32/45787_2.png) [@honmanyau](https://forum.freecodecamp.org/u/honmanyau)\
**Post date:** [April 12, 2017, 4:25pm UTC](https://forum.freecodecamp.org/t/access-array-inside-nested-object-solved/105355/4 "2017-04-12T16:25:36Z")

</div>

Hello there!

I think there are a few issues with your `for` loop. To begin with, you are already looping through each item in the for loop with `for (var prop in photos)`—the condition in the next `if` line is unnecessary and will always return true because prop is already something that can only exist in photo. Try the following code blocks and you will see what I mean:

```
for (var prop in photos) {
  console.log(prop); // Returns "fog", "partlycloudy", "cloudy", "sunny", "rain", "sleet"
}

```

The second problem that the way you used `find()` is not how `find()` is used—please have a look at the MDN documentation for more information ([link](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find)). Try this example and you will see what I mean:

```
console.log([1, 2, 3, 4, 5].find((element) => {return element > 3}));

```

`indexOf()` is probably what you are after.

Good luck! 🙂

**EDIT** : Looks like I was just a tad too late. ☹

---

<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:** [January 16, 2021, 5:52pm UTC](https://forum.freecodecamp.org/t/access-array-inside-nested-object-solved/105355/5 "2021-01-16T17:52:58Z")

</div>

Closed on January 16, 2021, 5:52pm UTC


