Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different between $.each and ko.utils.arrayForEach

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

I am using ko.utils.arrayForEach because I am working with knockout js but I just have curiosity.

like image 565
Xepe Avatar asked Aug 24 '12 16:08

Xepe


1 Answers

ko.utils.arrayForEach doesn't depend on jQuery and is simply 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 strict dependency of Knockout. If it is available, then there are a couple of places where Knockout will use it (string to DOM parsing and event handling).

$.each requires jQuery (obviously) and is intended to be way more flexible. It can iterate over properties in an object as well as arrays and work with jQuery objects. It also gives you an index while you are looping.

like image 144
RP Niemeyer Avatar answered Nov 15 '22 21:11

RP Niemeyer