iterate a jqlite array
Asked Answered
B

2

17

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.

Baltazar answered 28/9, 2013 at 17:14 Comment(0)
L
31

There is forEach()

angular.forEach(els, function(element){

});
Lemkul answered 28/9, 2013 at 17:25 Comment(0)
C
3

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.

Cambric answered 25/11, 2016 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.