forEach Array Method | JavaScript Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the array for each method executes a given function on every element from an array it sounds complicated so let's see how it works in code we have a numbers array containing all the numbers from 1 to 5 and let's say we want to console.log these numbers to the console we can call the for each method which requires a callback function that callback function will be given three parameters the value at the current step on the loop the index and the array which is called upon so let's create the function and give it here on the for each as a callback function let's call it console item or something like that so we get the item get the index and we get the array usually you don't really work with the array but well it's good to know that we receive it and here we can do console that flog item let's pass in now this function as a color back here and see what happens look at that we have one two three four and five we also get the index so maybe let's say something like a of item plus closing is equal to something like index let's see what this does we can say that a OS one oh it's the other way around so hey of index is equal to item okay my back so we can see now a of zero so the element at index zero it's one element at index one is two and so on and so forth now we can also provide this function we can actually write it as an arrow function so let's do that I'm going to copy this and put it inside here in line and cover this to an arrow function and now it should do the same thing but it's here in line usually this is how we'd write a function for the forage especially if this function is not called again let's also consume that logged array so you can see what the race stands for basically is the array on which is called upon let's see also some other ways we can use the forage method we can use it to calculate the sum of the numbers from the array so for that let's have a sum which will be initialized with zero and here in the 4-h we can call some plus equals item so keep in mind that this is every item from the array starting from the first one we now don't need this because we're not using them we can also remove the parentheses because we're having an arrow function and let's concept that lock the sum save and look at that we have 15 all the numbers are added up we can even add 10 15 25 and the function now works the same we get 65 another use case for this method is let's see how many times a letters appears in an array we're going to have an array of letters let's see a be a again be again see e and a again something like that and we're going to create an object which will keep track of the count of every letter so let's create an object here count and now inside of the for each callback method we're going to do something interesting we're going to do the following if the count of item if we have property inside the object with the item meaning a B or C or D then what we want to do that count item value to be plus equal one so we can also do a plus plus else it means that we don't have that property already so we can initialize it with one let's console our clock this count and see what we get oh my bad I called it upon the numbers array and we changed it so let's call it now you can see that we get nicely an object with four properties ABCD and we get three for a 2 for B 1 for C and one for D this is a nice use case of the for each method I hope you liked this tutorial and I'll see you in the next one bye [Music]
Info
Channel: Florin Pop
Views: 56,722
Rating: 4.8961039 out of 5
Keywords: javascript, javascript tutorial, javascript tutorial for beginners, array method, javascript arrays, higher order function, forEach, JavaScript forEach, forEach array method, forEach HOF, learn javascript, javascript array methods, forEach tutorial, learn forEach
Id: SXb5LN_opbA
Channel Id: undefined
Length: 5min 24sec (324 seconds)
Published: Thu Apr 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.