Hey, everyone, continuing with the last data type we need to learn in this video, we're going to
talk about objects.
So objects like iRace are also lists of elements.
But the difference is that objects are not ordered.
So in an array, you have elements in a specific order and you can access them by their indexes with
objects.
Each of the elements are going to be a key value pair separated by Callon.
So as you can see in this example, we have an object for an employee.
So this is the first element of this object.
So this element has a key, which is name and a value associated to this key, which is James Taylor.
The object keys are also called properties.
So this is the name property.
This is the year of birth property and so on.
Now, how can we access each of these values?
So let's copy this.
And create this object, as you can see, we create an object with the curly braces, so arrays are
created with braces and objects are created with curly braces.
So now that we have this object.
Let's try to do employee.
Dot lenth.
As you can see, we don't have the length property for objects, we also cannot get any value.
By trying to use an index like this, this is going to return undefined.
So how can we access each of the properties?
We just use their names inside braces.
So if we want to get the name, we just use name here.
Now we can get the employee's name.
So by this example, you see that this actually makes sense, because if we had an array for the employee
and we just put the name followed by the year of birth, followed by the ID, followed by the role,
it would be really hard to know what those values are referring to.
So we have an object.
We have names for the data, which is good for identification.
Besides accessing properties like this, we can also use the DOT notation, which is very common in
JavaScript.
So instead of putting braces, we just use a dot and then we type the name of the property without quotation
marks.
So if we do this, we can get the name.
We can also get the ID.
Just one thing to pay attention is that you can only use the DOT notation if the property follows the
variable naming rules.
So if you have a property that has a space or starts with a number or something like this, you won't
be able to use the DOT notation.
You will only be able to use this notation.
So I just put an example here with some odd properties.
This one follows the variable naming rules so I can access it with the dot notation.
The other ones I can't.
So this starts with a number.
This is a number, actually, and this one has a dash.
So these ones I can only access them like this.
So with objects, we can alter values assigned to properties and we can also add new properties.
So let's get this employee and let's add a roll for it.
Let me just take this to Visual Studio code so we have it there.
I'm going to comment this.
So shift out a.
So this is lesson 13 and we are talking about object.
OK, so I've just created this employee object.
And now I'm going to change the role.
So now this employee.
Will become.
The I.T. manager.
As you can see, I'm using a property that already exists.
So it is just going to replace the value.
If I create a property that does not exist, like.
Passport, then it is just going to add it to the object.
Let's just invent a passport, no, save this.
If we refresh the page, let's check how the employee object is.
We can see that now our employee is an I.T. manager and we have the passport number here.
As you can see, the passport was the last property to be added.
But still, it's not at the end.
It's not the beginning.
It's in the middle of the object.
So object properties, they don't have an order.
From now on, we're going to start working with multidimensional arrays and objects mixed together.
So I just put an example here.
So this is the coarsest array.
We can see that this is an array because of the Brace's.
So now we have to pay attention to these symbols so we can understand the structure of things.
So this is an array.
And how many elements do we have in the course array?
If we do courses that length, what are we going to get?
We can see that it has three elements.
So if we do cause that length, we will get three and each element of this array is an object.
So in this object we have the properties.
So the title of the course, the number of reviews, number of students, then we have this property
called catagories where you can have more than one.
So this is an array.
So this is the starting point of the work we are going to do with data.
This actually looks more like a piece of data that we would have to work on our website.
So like I said before, it's good to learn all the fundamentals first, because now when we see something
like this, we won't have any trouble understanding what this is and how can we work with each of these
elements.
So now just a small challenge, let me copy this.
I'm going to base it on Visual Studio Code.
So we have this crisis array.
Let me refresh the page.
Now, if I want to access this category, web development from the page, because how can we do this?
Well, first we start with the courses and now the prep course is the second element.
So the index number is one.
So by doing this, we can access the course, which is an object inside the page because we want to
get the categories so that categories or we can use Brace's and quotation marks.
Now we just access this array and now to get web development, which is the first element, we can just
do this.
So now we just reached this category in a multidimensional array.
So down here I've just included a few things worth remembering so we can work with multidimensional
arrays and objects without a problem.
So arrays are identified by braces, arrays are ordered.
Lists of values separated by comma.
Objects are identified by curly braces, objects.
Elements are key value pairs separated by Callon and objects.
Elements are separated by comma.
So inside an object, we have a key value pair separated by Callon and these each of the elements are
going to be separated by comma.
So now we can understand what's going on with this array.
So by looking at this, we can conclude that the courses variable is an array containing three elements
we can see by the Brace's, the elements of the courses array are objects we can see by the curly braces.
Each of these objects have four elements, which are key value pairs.
The objects have values of different data types.
So we have the title property that has a string associated to it.
We have the reviews and students which are numbers and we have the categories, which is an array.
So that was all I wanted to talk about objects for the moment.
In the next video, I'm going to go back to this subject to talk about objects methods.
I'll see you then.