The difference between $ .each and ko.utils.arrayForEach

I am trying to understand the difference between $ .each and ko.utils.arrayForEach, is there something more that iterates over the array?

I use ko.utils.arrayForEach because I work with js knockout, but I just have curiosity.

+7
source share
1 answer

ko.utils.arrayForEach is independent of jQuery and just a shortcut to writing something like:

 for (var i = 0, j = myArray.length; i < j; i++) { someMethod(myArray[i]); } 

It only works with arrays. jQuery is not a strong knockout dependency. If it is available, then there are several places where Knockout will use it (a line for parsing the DOM and handling events).

$.each requires jQuery (obviously) and should be more flexible. It can iterate over properties both in an object and in arrays and work with jQuery objects. It also gives you an index during a loop.

+14
source

All Articles