Welcome to tutorial number 11 of the Easy jQuery series, part of EasyProgramming.net. In today's tutorial, we'll learn how to work with the .each() method in jQuery to loop through arrays.
You can write the .each() method twice, watch the video to learn both ways! It's very similar to the .forEach() method in JavaScript so if you understand that, this will be a piece of cake.
The .each() loop can be written two different ways. The frst way lets you select an array object and pass in a callback function as the one and only argument as shown below:
$(array).each(function(index, item){ execute code; the 'item' is the actual value; 'index' is the index number (e.g. i); })
The second way lets you select jQuery as the object itself and pass in two arguments. The first being the array and second being the callback function.
$.each(array, function(index, item){ execute code; the 'item' is the actual value; 'index' is the index number (e.g. i); });
I hope you enjoyed this tutorial! Please feel free to ask questions below or just leave your comments.
To fork the fiddle and follow along: https://jsfiddle.net/easyjs/arxejy83/
Remember to checkout the Resources section below for associated downloadable content, JSFiddle links, and other resources. Watch the video and follow along!