standup75
standup75

Reputation: 4814

iterate a jqlite array

Is there cleaner way to iterate over a set of jqlite elements than the following?

var els = angular.element(document.getElementById("main").children); // just as an example
for(var i=0, el; el=els[i]; i++) {
  // do something
}

I mean, there is nothing wrong with this, I am just surprise that there is no each() or forEach(). And I could not find anything about that on the internet.

Upvotes: 17

Views: 6753

Answers (2)

mik01aj
mik01aj

Reputation: 12392

You can use Array.prototype.slice.call(jqResult) to convert it to a normal Array, that has supports forEach, map and filter operations.

See also this reference from MDN.

Upvotes: 3

zs2020
zs2020

Reputation: 54542

There is forEach()

angular.forEach(els, function(element){

});

Upvotes: 31

Related Questions