Let's get started with a ray of object for this particular section.
We have covered it in a previous section and the arrays and object.
So the difference here is about the data structure.
So instead of using single values like numbers and strings for this particular section, we are going
to deal with arrays of objects.
So all the methods that we use in the pivot section talking about MapReduce and filter, that's what
you are going to use it in this particular video.
So to get most out of this section, our advice to you to make a review on the arrays section.
But in case you are comfortable with ray meters then let's get started.
Barrister I'm going to explain everything I write, so let's get started.
Like I said, we are going to repeat ourselves by the data structure that is going to be different in
this particular section.
So the first one that we are going to talk about is called for each.
For each.
It.
The data that we are going to work on is a race of student that is students equal to array of students
going to be arrays of object.
So for the first students, let's give it a name of John comma and the age is going to be 20.
So let me copy this one and repeat it three times.
I want to have four students.
Come on, three, save it.
And lastly, let's paste this one, two to change this one to Mary.
And the age is 26 years and this one is Peter and the age is also 22 years and this one going to be
Sally and the age is going to be 23.
So this is a data that we are going to work on.
So now let's talk about for each well the for each method looks through an array and execute a function
for each item in the particular array and for each does not mutate the original array as we discussed
in previous video.
So let's say that we have lists of student and we want to look through and display the names only we
can use for each to do that.
So here you go.
So as before, the same syntax, but the data structure that differs here.
So I would take my students array on that.
We have for each method on that and we call it as that.
So at this point we can use normal function or arrow function.
So here we go.
Let's use a function this time around.
And for each it takes in three arguments.
Remember, one is an actual item and then one is an index of the item and the next is the origin array.
So here I can console.log each of these ones.
Let's say this the item here and I'm going to be the item.
So let me paste this one two times.
It's going to be the index.
Index going to be the index.
That's the index of the individual items.
Lastly, going to be the origin array as the origin array.
I start when I save it and I check my console.
Indeed, let me refresh it.
And now we say that the array has printed how many times.
One, two, three, four.
Right.
So it means that we have four records in the array.
So this array will repeat it four times.
Let's say we have only one record in this array.
It means that this array is going to repeat only one time.
So have a look.
If we see that we have only one time being looped and the first one is the item which represent the
individual student in the array.
That's what we call the item.
And the index determined the position of this particular item in the array because the first one, that's
where we have what the first index of zero and the original array is that if our it indeed we have only
one record in the array.
So let's bring back all the data in the array so you can see that we have four that have been looked
in.
Great.
So for this one we can see that I want to display only the name of H.
That is why for each comes into play.
So let's say that we have a front end application and want to display this one to the browser we can
use for each and then create the element and just inject it into the DOM for us to see inside the browser.
So for this agreement, the most required one is the item, the first one.
The other ones are optional, right?
But in case you want to delete, let's say a data, they will need what is called an index.
But ideally we don't even bring in the original array here.
And also in most cases we don't use the the index.
For example, if you are dealing database, we have the ID of the record only.
So there is no need to get the ID here or using the index as an ID.
So let's remove that.
And now we have all the item that is a user in my array.
So for this one I can get the name only.
So a name is going to be item dot name.
So here let's make it as user because it's a user.
Let's make it more descriptive here.
That is it.
So I can also copy this one.
And then.
Carpet is one piece and I want the age of the user.
So here you go.
Going to be each of the user if I save it.
You see that I have them nicely being displayed here.
So that is it.
About the four h.
Like I said, if you go to the dot manipulation, we are going to loop through data like this one and
then display it inside the browser for users to see that they're displaying it inside the console.
As you move on, if you get to the manipulation, you can see how important this method is.