0

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?

6
  • 3
    There's a .forEach() function on the native Array prototype.
    – Pointy
    Sep 11, 2012 at 12:49
  • 3
    what's wrong with a for loop? (other than your implicit global i)
    – jbabey
    Sep 11, 2012 at 12:50
  • 1
    For arrays: arr.forEach( fn );. For (non-array) objects: Object.keys( obj ).forEach( fn );. Sep 11, 2012 at 12:52
  • @jbabey--Nothing per se, I just like to abstract things when I can do so without losing performance. It's just personal preference; to me the code becomes more readable. Sep 11, 2012 at 12:56
  • there is also for( i in myArray ){ alert( i + ': ' + myArray[i] ); }
    – alexbusu
    Sep 11, 2012 at 13:00

5 Answers 5

4

You have forEach() in EcmaScript 5 : https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach

But it's not comptatible with IE < 8.

So you may need to use this lib to be supported by all browsers : https://github.com/kriskowal/es5-shim

3
  • Ok, thanks all! I can't accept this answer for another 10 minutes. . . Sep 11, 2012 at 12:52
  • @Pointy--Excuse my ignorance, but does the fact that I am in Node.js make this answer incorrect (I am seriously asking, not being a smart a$$). Sep 11, 2012 at 12:53
  • @MatthewPatrickCashatt It means that you don't have to worry about IE :) Sep 11, 2012 at 12:55
0

ECMA Script 5 incorporates a method of Array called forEach. Usage is the same as jQuery.each for Arrays.

There is no need to use a for...in loop anymore, as Array.forEach is easily implemented (see the MDN Documentation link, middle of the document).

To clarify, forEach will loop through arrays only, not objects nor jQuery objects.

4
  • 1
    No, that's not quite true - the arguments passed to the callback are different between $.each() and the native .forEach().
    – Pointy
    Sep 11, 2012 at 12:51
  • except that it's only a method of arrays, which makes it completely different than jQuery.each
    – jbabey
    Sep 11, 2012 at 12:52
  • I meant to say that usage is the same for Arrays only. 2 downvotes really necessary?
    – Austin
    Sep 11, 2012 at 12:53
  • @Austin I didn't downvote but the usage really isn't the same - .forEach() passes (value, index, array) to the callback, while $.each() passes (index, value).
    – Pointy
    Sep 11, 2012 at 13:14
0

You could use

for( i in myArray ){ 
    alert( i + ': ' + myArray[i] ); /* your actions here */
}
6
  • 1
    You would also want to check myArray.hasOwnProperty(i) inside of your for loop... But this necessitates much more code than using Array.forEach
    – Austin
    Sep 11, 2012 at 13:03
  • It's a bad idea to use for ... in on arrays.
    – Pointy
    Sep 11, 2012 at 13:14
  • @Pointy, even when you know what array you're dealing with? :)
    – alexbusu
    Sep 11, 2012 at 13:16
  • @AlexanderV.B. yes - you're not even guaranteed to get the elements in numeric-index order.
    – Pointy
    Sep 11, 2012 at 13:18
  • 1
    Be careful with w3schools. The other article is pretty bad too - JavaScript doesn't have "associative arrays". It may work now, but the spec does not stipulate any order to the iteration in a for ... in loop. It's for iterating over properties of an object, which have no inherent ordering anyway.
    – Pointy
    Sep 11, 2012 at 13:28
0

jQuery is also available for node incase you'd want to stick to your habit.

npm install jquery
0

You might want to look into the async library if your objective is to use the foreach in node.js. This allows you to parallelize your foreaches.

https://github.com/caolan/async

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.