Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 414
standup75 Avatar asked Sep 28 '13 17:09

standup75


2 Answers

There is forEach()

angular.forEach(els, function(element){

});
like image 88
zs2020 Avatar answered Oct 07 '22 00:10

zs2020


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.

like image 21
mik01aj Avatar answered Oct 07 '22 01:10

mik01aj