Hi and thanks for looking!
Background
I am building my first application in Node.JS and am replacing jQuery-based habits with native JavaScript.
For most items, this is trivial; however, I am finding that I really miss the elegance of jQuery's $.each()
abstraction and I would like to do something similar without relying on jQuery.
Unfortunately, the same code in JavaScript is a bit cumbersome:
myArray = ["item 1", "item 2", "item 3"];
for(i=0; i<myArray.length; i++) {
myArray[i] = "Do Something Here".
};
What I would like to do is have something like this:
myArray = ["item 1", "item 2", "item 3"];
myArray.each(function(){
//do stuff
});
Question
Is there already an extension method out there for accomplishing this? I can't help but think I am re-inventing the wheel. If there isn't anything out there, would an extension method be best?
.forEach()
function on the native Array prototype.i
)arr.forEach( fn );
. For (non-array) objects:Object.keys( obj ).forEach( fn );
.for( i in myArray ){ alert( i + ': ' + myArray[i] ); }