This course has been updated! We now recommend you take the JavaScript: From First Steps to Professional course.

Check out a free preview of the full JavaScript: From Fundamentals to Functional JS course:
The "Nesting Exercise Solution" Lesson is part of the full, JavaScript: From Fundamentals to Functional JS course featured in this preview video. Here's what you'd learn in this lesson:

Bianca walks through the solution for the Nesting Exercise.

Get Unlimited Access Now

Transcript from the "Nesting Exercise Solution" Lesson

>> [MUSIC]

>> Bianca Gandolfo: So let's just go through these quickly. So here we are creating just an empty array, and we're assigning it to the variable friends. Friends here we're pushing from our animals collection. The first, the very first value, which is an object that has a property username. So we're adding the username property.

[00:00:31] In this case, we can see that it's Mittens from the previous exercise, as well as the second, or the first index, really, value which is DaffyDuck. And notice that for this syntax, for the .push, you can actually pass in more than one value and it will just add both of them in order.

>> Bianca Gandolfo: Cool. And so here's our relationships objects. So we are in our relationships object, we can have different types of relationships. So one of those might be our friends relationship, and our friends again, is just our object with Mittens and DaffyDuck, and now our relationship object has a property called friends with the value of an array that looks like this.

[00:01:29] And we can inspect that, or actually we can't, cuz I don't have previous. So when you've consoled out log relationships, it's gonna look like this object, and if you wanted to count the keys, you could use this handy object.keys method. It will return an array of all the properties, and then you could take the .length.

[00:01:53] And so we see here that our object, a relationship object has one key, friends, and therefore our length is then one. So, we have our friends and then we also have our matches. So our matches is an empty array. And here we are putting our matches array, empty array, onto our relationship object add a property name called matches.

[00:02:25] And this is again with our nested, we keep adding, adding on our commands. So the first command would be here's our access our relationships object, the second one would be access that matches property, and then the third one would be to push this value pig onto that. Then we can console.log(relationships) and we can see now that we have a few things in our relationship object.

[00:02:58] Let's wrap it. So we have our friends, which is in array with two names and then we have our matches, which is an array of one with pig. And then if you wanted to loop through your animals or your farm, you would just do your regular loop and animals[i].relationships, your gonna add that relationship object and you can console.log your animals and inspect the relationship object.

[00:03:28] Keep in mind though that this is gonna always be the same relationship object, so all of your animals in your farm are actually gonna have the same friends and the same relationships. And that's an interesting thing to inspect and change things around, and see how it changes in all of them.

[00:03:42] And again, that's only true for things like array objects and functions. For strings, numbers, Booleans, all those primitive values that is not going to be true. Awesome, so any questions about what I just went over?
>> Bianca Gandolfo: No? Okay. Awesome, so that kinda wraps it up for today. If you want the solutions, there's a link in the slides.

[00:04:14] Submit your email, there's an option to opt in for notifications about upcoming classes taught by me or some of my colleagues at Hack Reactor if you'd like. Either way you could have the solutions, and also there's a link for live classes that we have monthly at Hack Reactor.

[00:04:31] We are going to do this class actually on Sunday, so if you know anyone in the San Francisco area that's interested, there's a few spots left there. And yeah, and you could just check the Meetup up for that. Awesome. Thank you guys so much for being here. I really appreciated your time and attention, and I hope that you got a lot out of this class and that you can continue growing with your JavaScript.