This video I'm going to take a little bit of time to compare the syntax between arrays and objects and
Javascript.
So let's start by comparing them conceptually.
Remember that an array is used to store a list of data and we add things to an array.
There's a very specific and particular order.
Every item is bound to an index that we refer to it by the first item is bound to index 0.
The next item is bound to index 1 and so on.
So an object on the other hand is not a list.
There is no particular order I'd like to imagine objects is just a gelatinous blob with a bunch of things
floating around inside.
So key value pairs that's really important.
And in some languages objects are called dictionaries.
And the reason that I bring that up is that I think the idea of a dictionary is helpful here.
If you think of a dictionary it has a bunch of key value pairs.
It has words and then corresponding definitions.
So if I want to know the definition of a word let's say I'm helping my grandma out.
She doesn't know what l o l means.
But we can open up the dictionary find L-O and I can finally help my grandma understand that Ella well
doesn't mean leg of lamb but it instead means laugh out loud.
So again dictionary if that helps you remember it.
We'd look something up based off of a key and we get a corresponding value.
Let's now start comparing the syntax so I'm going to go over to the array's window and make a new array
of what else but dugs.
So var dogs equals an empty array and I'm going to just fill in some data here so we'll go with Rustie
of course lucky and Bubba and I hit enter.
And that makes me an array with three items in it.
Let's compare that to making an object.
So to make an object we will make a single dog represented in an object and we can do this all in one
line like we did with the array.
But it gets very long so the convention and what you see most often is that people break it up into
separate lines.
Our dog will just have two pieces of information.
Name and breed.
So let's add name.
Name will be Bhabha and breed will be lab.
Hit enter and we can retrieve dog and we see that we have name as Bubba breed as lab.
So again the syntax a key without quotes.
We can have quotes there but you almost never see that Colon a value which can be a string but remember
it can be anything can be a number it can be another object you can be an array a boolean undefined
no any possible value can go here.
So next I'd like to compare the syntax to access data out.
So let's go to the array and let's say I want to retrieve the second item I want to get lucky out of
theory.
So to do that I need to know has index 0 1 index of 1.
So dogs square bracket 1 and I got lucky and I didn't even have to stay up all night.
So let's contrast that with how we access an individual property out of an object.
Remember there are two different ways we can do it.
Let's access Bubba.
So I need to type name and the first one which is most array like at square brackets with a property
name.
And that's going to give me bubba but the other version is to use the dot notation dog name and I get
Bubba.
So both of them do the exact same thing.
Remember either one comparison that I think helps here is that an array is just a very special type
of an object where the keys are always numbers.
So it is still a key value pair are key value pairs between 0 and Rustie one and lucky two and baba
.
Over here we have key value pairs where we have name as the key and Baba is of value freed as the key
and lab as a value.
So again you can think of an array as just a special version of an object and actually behind the scenes
it actually is an object which we'll see later on.
Next let's talk about adding new data in after the fact.
I can use methods like push or shift or I can do it manually by figuring out what index I want to add
something to.
Let's just use push so I'll do dogs up push and I'll push in a dog Wyatt and we look at dogs and we've
added in a new element to do that to our dog object if we want to add in the property age.
I don't need a special method like push or upshift because the whole point of those array methods is
to work with the order of the array to add something to the end or to the beginning.
But because there is no end or beginning in an object all you have to do is say dog and I can use this
syntax.
Age equals six.
Or I can do dog dog.
Age equals six.
And I look at dog and we get this object with name Bubba breed lab.
In the last key value pair that we just added.
Age 6.
So this is a big difference.
To add something in.
I just can do it arbitrarily assign any key that I want and any value that I want and they'll automatically
be added in for me on an array.
I need to specify exactly where it's added in at the end of the array.
The beginning the middle index 1000.
It's up to me.
Lastly let's talk about updating items.
Let's take our array and change lucky to Lucy to do that.
I just need to find a lucky first which is dogs one that gives me lucky and then I reassign the value
of dogs one to Lucy on an object.
It works very similarly.
Let's change the breed to black lab.
So I first need to retrieve dog dog breed and then reset it to be black lab just like that.
And if I look at dog now we see breed has a value of black lab.
So let's sum up the big differences here.
Both of them are arrays and objects use key value pairs but arrays are a special subset of key value
pairs where the keys are always numbers and they're always an order an object.
The key can be anything.
It doesn't matter if it's a number or if the key is name or breed.
It's arbitrary and there is no special order