for each in JavaScript
I felt that this deserves a tad more attention than just adding this to my del.icio.us bookmarks, but just moments ago I stumbled onto a JavaScript reference page on Mozilla’s developer center that spoke of this intrigue about for each ( … in … ). I had no idea this existed! I looked over at Dan Webb who’s sitting right across from me, and he agreed, it’s news to him too. Granted it was implemented in JavaScript 1.6 (which means it will work in Firefox 1.5), but as far as I knew, these were the only things new in JavaScript 1.6.
Anyhow, it’s nothing that special. It simply allows you to iterate over the values of an object, and not the keys. Nevertheless, it’s nothing you can’t already do with just a regular for (in) which returns you the keys of an object… so if you have the keys, and the object… you indeed, have access to the values. Go ahead, have a look.













May 25th, 2007 at 10:02 am
I’m confused unless it’s just thats it’s been updated since you posted the second link you gave “<a href="http://developer.mozilla.org/en/docs/New_in_JavaScript_1.6">this link</a>” has foreach there 3rd one down in “iterative methods”… Have I missed something?
May 25th, 2007 at 11:38 am
David, note the difference between
forEach, andfor each (in). The former is an Array method, and the latter is a statement. Take a peak at the 1.5 reference guide.May 25th, 2007 at 2:19 pm
Ah Cool I thought I must have misinterpereted, my bad.
May 25th, 2007 at 8:37 pm
Since I found jQuery, JavaScript is not the same to me, rather than programming with JavaScript I tend to program on top of jQuery.
Libraries take over the language, sometimes takes over other libraries, for example I find your library easier to digest than the YUI library it self. It is the era of libraries and frameworks I guess.
May 26th, 2007 at 4:21 am
For the record,
$.eachorArray.prototype.forEachare not the same asfor each (...in...). Please read this post carefully.May 26th, 2007 at 8:15 am
The new things in Javascript are only added to FireFox, but Opera and especially IE don’t have them(unfortunately) :( .
May 27th, 2007 at 6:07 pm
The confusion is because
for eachis part of the ECMAScript for XML specification, which was implemented along with JavaScript 1.6. Check out the spec (http://www.ecma-international.org/publications/standards/Ecma-357.htm) and go to page 63, section 12.3. Of course, just becausefor eachis part of E4X doesn’t mean that it can’t be used in normal JavaScript as well. It’s a little confusingly named but otherwise a nice addition to the language.May 28th, 2007 at 4:02 am
Sorry for the misunderstanding. I wasn’t saying this is the same as $.each. Just that, due to other available methods to get things done, actual capabilities/functions of are overlooked by guys like me. So, thanks for pointing out the right stuff.
May 29th, 2007 at 7:47 am
Holy crap! Where has this been hiding? Awesome. I’m using this today. Great find, thanks D.
June 7th, 2007 at 7:20 am
Great find Dustin, quick question does Mozzila plan on making translations available in other languages other than Fench anytime this year?
June 13th, 2007 at 2:40 am
same here. iliketitallyloveit. well, admittedly after running into the above mentioned “forEach” and “for each” trap…
i know, girls and coding. ;) but now its working fine#
cheers,
eva
June 20th, 2007 at 6:47 pm
Hey Dustin,
There’s an equally nifty one which is sort of an alliteration of the for loop,but takes a similiar style as the for each you mentioned above. Have a look !
// general usage for (var obj1 in obj2){ … } //example 1 for (var obj in window){ document.write( ‘window.’+obj +’ =>’+ window[obj] ); } //example 2 var a={name:’asd’,id:123} , b; for(var obj in a){ b = typeof a[obj]; console.log( b +’=>’+obj+’=>’+ a[obj] ); }Keep Clicking,
Bhasker V Kode,
returnable.org
PS: i think that you can seriously contemplate adding a ‘preview comments’ feature!
July 23rd, 2007 at 1:31 am
Greaet find indeed :)
November 27th, 2007 at 3:34 am
Its very reasy when you use JQUERY (search google)
its like this:
December 31st, 2007 at 11:42 am
Sadly, I found it does not work in IE. Darn IE.
April 30th, 2008 at 7:34 am
Am I the only one who finds it annoying that Javascript still requires a traditional “for” loop to iterate an Array?
Why include a “for…in” construct that doesn’t let you iterate an Array?
Even prototype “.each” doesn’t yield good performance. Sad.