Hi all, when I want to interate through an array, all those pesky Prototype functions get in my way! When I do this: --------------------- r=""; for(x in new Array()) { r+="var: "+x+", val: "+ (new Array())[x]+"\n"; } alert(r); --------------------- I get a giant alert window with loads of functions printed out. Is this a bug, or is there a better way to iterate over arrays? __________________________________________________________________________ Erweitern Sie FreeMail zu einem noch leistungsstarkeren E-Mail-Postfach! Mehr Infos unter http://freemail.web.de/home/landingpad/?mc=021131
x = [1,2,3]; x.each(function (i) { document.write(i); }); On 11/4/05, Martin Scheffler <wooyay-S0/GAf8tV78@public.gmane.org> wrote:> > Hi all, when I want to interate through an array, all those pesky Prototype functions get in my way! > > When I do this: > --------------------- > > r=""; > for(x in new Array()) { > r+="var: "+x+", val: "+ (new Array())[x]+"\n"; > } > alert(r); > > > --------------------- > I get a giant alert window with loads of functions printed out. > Is this a bug, or is there a better way to iterate over arrays? > __________________________________________________________________________ > Erweitern Sie FreeMail zu einem noch leistungsstarkeren E-Mail-Postfach! > Mehr Infos unter http://freemail.web.de/home/landingpad/?mc=021131 > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >
The for ... in ... loop in JavaScript iterates through an objects properties, which aren''t necessarily only array items. Use the way Jon has proposed. Thomas Am 03.11.2005 um 21:55 schrieb Martin Scheffler:> > Hi all, when I want to interate through an array, all those pesky > Prototype functions get in my way! > > When I do this: > --------------------- > > r=""; > for(x in new Array()) { > r+="var: "+x+", val: "+ (new Array())[x]+"\n"; > } > alert(r); > > > --------------------- > I get a giant alert window with loads of functions printed out. > Is this a bug, or is there a better way to iterate over arrays? > ______________________________________________________________________ > ____ > Erweitern Sie FreeMail zu einem noch leistungsstarkeren E-Mail- > Postfach! > Mehr Infos unter http://freemail.web.de/home/landingpad/?mc=021131 > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Thomas Fuchs wrote:> The for ... in ... loop in JavaScript iterates through an objects > properties, > which aren''t necessarily only array items. > > Use the way Jon has proposed.Problem is that you can''t expect each and every 3rd-party JS library that lives in the browser page to use that way. This has been a showstopper for me. Sylvain -- Sylvain Wallez Anyware Technologies http://people.apache.org/~sylvain http://www.anyware-tech.com Apache Software Foundation Member Research & Technology Director