So one of the thing that we'll see pretty commonly going forward is the use of objects inside of arrays
or arrays instead of objects.
Let's say that I'm building an app where I have posts like a blog post and each post has comments.
Let's start by representing.
Let's talk about how we might represent that data.
So this is going to be a complex structure here.
I'm going to clear out my cons..
And this is what it will look like.
So our Web site will have an array of posts so each post will actually be its own object and we'll have
properties like title and author and then maybe comments or votes or likes or something.
So we're going to start by doing an array of posts.
And rather than just a post being a string like hello like we have seen with dogs so far we just did
strings.
We're actually going to make each post an object
and each post object will have its own properties like title cats are mediocre.
I do feel like it can say that I've owned a few cats and honestly my experiences were very underwhelming
so I'll alter that post and this is our first item in the array so post index 0 gives us this entire
object.
Let's do one more
and this time we'll have another post.
Cats are actually awesome.
And this post was not written by me.
It was written by cat lover.
All right so we can hit enter here and this will make an array of posts.
Each post is actually its own object so this is a kind of structure that will see all the time or we
have a list and each item in that list is its own object whether it's an array of comments or an array
of posts or an array of friends where each one is its own object.
When we build web apps we'll see this all the time.
So just to show you one more thing that we can do.
We could actually embed an array of comments instead of each individual post so we could have comments
as a key and the value would be an array.
And this is where it gets a little crazy.
We could have two comments.
First one will be some post and the second one will be terrible post.
We can do the same thing down here.
Add another key which is called comments and the value is an array and the first one will just be somebody
commenting a heart and the second one will be go to hell.
I hate you just have to be realistic here.
These are the kind of comments you get all the time and if we hit enter here we now have our complete
data structure.
So let's open up posts or just type it into what we get.
We have an array with two items in it.
So posts length two items each one is an object and each one of those post objects has an author comments
which is an array and title which is a string.
So if I want to access out cats or mediocre the title of the first post I need to do posts zero and
then I need to access title.
So post zero title that gives me.
Cats are mediocre.
If I wanted to access the second comment of the second post.
So that is going to be the second post.
Open up comments.
The second comment this very angry realistic one.
Go to help.
I hate you.
I'm going to need to go ahead and open up posts.
The second one index one and then dumb comments.
It's going to give me the array and then I want the second item which is index of 1 and we get go to
hell I hate you so this is an important skill.
Being able to take a data structure like this and this one is actually not too bad.
But we will see some very very complex data structures later on where things are nested.
Five six seven levels deep and you need to be able to eventually at least go through an access one layer
at a time like we did here.
So hopefully this video helps you understand the differences between arrays and objects and also prove
to you that we often use them in conjunction together.