The two methods seem to have something to do with the name, but the difference between them is quite large in JavaScript.
foreach () is used for the operation of an array, which executes the established function for each element in an array (not arrays cannot use the foreach () method). and $.each () is a method in jquery that executes the function that is developed for each matching element in the collection. In addition, they correspond to the same parameters in the callback function: Array.foreach (ITEM,INDEX,ARRAY1); $ (selector). each (function (index,element)).
Here is the $.each () Example:
var lst = $ (". Distance"); $ (LST). each (fun); function Fun (index,item) { alert (a). Text ()); }
var arr = [1, 2]; $ (ARR). each (fun); function Fun (index,item) { alert (item); }
The following is an example of a foreach ():
var arr = [1, 2]; Arr.foreach (fun); function Fun (num) { alert (num); }
So where you use foreach, you can use $.each instead. You can also use the most basic for loop for the operation of the elements in the collection.
The difference between each () and foreach () in Javascript/jquery