function CreateTOC(){ var $aTOC = document.getElementsByClassName(''tocitem''); var $temp = ''''; if(isArray($aTOC)){ for($t in $aTOC){ $temp += ''t='' + $t + '', '' + $aTOC[$t].name + ''<BR>''; } $(''test1'').innerHTML = $temp; } } When I run this code as you see it, this is the result: t=indexOf, undefined t=without, undefined t=flatten, undefined t=compact, undefined t=last, undefined t=first, undefined t=clear, undefined t=_each, undefined t=_reverse, undefined t=entries, undefined t=member, undefined t=select, undefined t=find, undefined t=map, undefined t=inspect, undefined t=zip, undefined t=toArray, undefined t=sortBy, undefined t=reject, undefined t=pluck, undefined t=partition, undefined t=min, undefined t=max, undefined t=invoke, undefined t=inject, undefined t=include, undefined t=grep, undefined t=findAll, undefined t=detect, undefined t=collect, undefined t=any, undefined t=all, undefined t=each, undefined t=0, #Assessments Instruments t=1, chiefcomplaint t=2, History of Present Injury The only tocitem''s are the last three in that list. Where is the rest of that array coming from? _____________________ Thanks, Keith Davis - MCSA, A+, N+ P.R.I.D.E. - Director of Computer Services www.pridedallas.com Work (214) 351-6600 ext. 119 Mobile (214) 906-5183 _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
You are using Array where you should just be using an Object... don''t use Array as an associative array, there is really no point whatsoever in doing this. If you aren''t using .push, .pop, .splice, and the numeric based indexing of the Array object... then use just a plain old Object. On 6/20/06, Keith Davis <keithdavis-mQJYS7++JhLwMB+CN6uaIQ@public.gmane.org> wrote:> > function CreateTOC(){ > > var $aTOC = document.getElementsByClassName(''tocitem''); > > var $temp = ''''; > > if(isArray($aTOC)){ > > for($t in $aTOC){ > > $temp += ''t='' + $t + '', '' + $aTOC[$t].name + > ''<BR>''; > > } > > $(''test1'').innerHTML = $temp; > > } > > } > > When I run this code as you see it, this is the result: > > t=indexOf, undefined > t=without, undefined > t=flatten, undefined > t=compact, undefined > t=last, undefined > t=first, undefined > t=clear, undefined > t=_each, undefined > t=_reverse, undefined > t=entries, undefined > t=member, undefined > t=select, undefined > t=find, undefined > t=map, undefined > t=inspect, undefined > t=zip, undefined > t=toArray, undefined > t=sortBy, undefined > t=reject, undefined > t=pluck, undefined > t=partition, undefined > t=min, undefined > t=max, undefined > t=invoke, undefined > t=inject, undefined > t=include, undefined > t=grep, undefined > t=findAll, undefined > t=detect, undefined > t=collect, undefined > t=any, undefined > t=all, undefined > t=each, undefined > t=0, #Assessments Instruments > t=1, chiefcomplaint > t=2, History of Present Injury > > The only tocitem''s are the last three in that list. Where is the rest of > that array coming from? > > *_____________________* > *Thanks, * > > *Keith Davis - MCSA, A+, N+* > *P.R.I.D.E. - Director of Computer Services* > ***www.pridedallas.com*** > *Work (214) 351-6600 ext. 119* > *Mobile (214) 906-5183* > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > >_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
For your example.. use the numeric index to access the ACTUAL items in the Array... not the properties of the instance (Object) of the array... for (var i = 0; i < $aTOC.length; i++) { $temp += ''t='' + $t + '', '' + $aTOC[i].name + ''<BR>''; } On 6/20/06, Ryan Gahl <ryan.gahl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > You are using Array where you should just be using an Object... don''t use > Array as an associative array, there is really no point whatsoever in doing > this. If you aren''t using .push, .pop, .splice, and the numeric based > indexing of the Array object... then use just a plain old Object. > > On 6/20/06, Keith Davis <keithdavis-mQJYS7++JhLwMB+CN6uaIQ@public.gmane.org> wrote: > > > function CreateTOC(){ > > > > var $aTOC = document.getElementsByClassName(''tocitem''); > > > > var $temp = ''''; > > > > if(isArray($aTOC)){ > > > > for($t in $aTOC){ > > > > $temp += ''t='' + $t + '', '' + $aTOC[$t].name + > > ''<BR>''; > > > > } > > > > $(''test1'').innerHTML = $temp; > > > > } > > > > } > > > > When I run this code as you see it, this is the result: > > > > t=indexOf, undefined > > t=without, undefined > > t=flatten, undefined > > t=compact, undefined > > t=last, undefined > > t=first, undefined > > t=clear, undefined > > t=_each, undefined > > t=_reverse, undefined > > t=entries, undefined > > t=member, undefined > > t=select, undefined > > t=find, undefined > > t=map, undefined > > t=inspect, undefined > > t=zip, undefined > > t=toArray, undefined > > t=sortBy, undefined > > t=reject, undefined > > t=pluck, undefined > > t=partition, undefined > > t=min, undefined > > t=max, undefined > > t=invoke, undefined > > t=inject, undefined > > t=include, undefined > > t=grep, undefined > > t=findAll, undefined > > t=detect, undefined > > t=collect, undefined > > t=any, undefined > > t=all, undefined > > t=each, undefined > > t=0, #Assessments Instruments > > t=1, chiefcomplaint > > t=2, History of Present Injury > > > > The only tocitem''s are the last three in that list. Where is the rest of > > that array coming from? > > > > *_____________________* > > *Thanks, * > > > > *Keith Davis - MCSA, A+, N+* > > *P.R.I.D.E. - Director of Computer Services* > > ***www.pridedallas.com*** > > *Work (214) 351-6600 ext. 119* > > *Mobile (214) 906-5183* > > > > > > _______________________________________________ > > Rails-spinoffs mailing list > > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > > > > > >_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Prototype modfies the core Array object and you shouldn''t use any for .. in -loops, instead do like this: [...] for (var i=0; $t = $aTOC[i]; i++) { $temp += ''t='' + $t + ''<br />''; } [...] or a more prototypish way: $("test1").innerHTML $A(document.getElementsByClassName(''tocitem'')).inject("", function(iterator, element) { iterator += element + "<br />"; return iterator; }); Ciao! Martin -- burnfield.com/martin On 6/21/06, Keith Davis <keithdavis-mQJYS7++JhLwMB+CN6uaIQ@public.gmane.org> wrote:> > > > function CreateTOC(){ > > var $aTOC = document.getElementsByClassName(''tocitem''); > > var $temp = ''''; > > if(isArray($aTOC)){ > > for($t in $aTOC){ > > $temp += ''t='' + $t + '', '' + $aTOC[$t].name + ''<BR>''; > > } > > $(''test1'').innerHTML = $temp; > > } > > } > > > When I run this code as you see it, this is the result: > > t=indexOf, undefined > t=without, undefined > t=flatten, undefined > t=compact, undefined > t=last, undefined > t=first, undefined > t=clear, undefined > t=_each, undefined > t=_reverse, undefined > t=entries, undefined > t=member, undefined > t=select, undefined > t=find, undefined > t=map, undefined > t=inspect, undefined > t=zip, undefined > t=toArray, undefined > t=sortBy, undefined > t=reject, undefined > t=pluck, undefined > t=partition, undefined > t=min, undefined > t=max, undefined > t=invoke, undefined > t=inject, undefined > t=include, undefined > t=grep, undefined > t=findAll, undefined > t=detect, undefined > t=collect, undefined > t=any, undefined > t=all, undefined > t=each, undefined > t=0, #Assessments Instruments > t=1, chiefcomplaint > t=2, History of Present Injury > > The only tocitem''s are the last three in that list. Where is the rest of > that array coming from? > > _____________________ > Thanks, > > Keith Davis - MCSA, A+, N+ > P.R.I.D.E. - Director of Computer Services > www.pridedallas.com > Work (214) 351-6600 ext. 119 > Mobile (214) 906-5183 > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > >
Technically, as per the JavaScript spec (that is, the ECMAScript spec), the for/in loop iterates over the properties of an object, _not_ the items of an array. As Prototype extends the Array prototype with all sorts of useful functionality, you''ll get these additional properties because of the for/in loop used incorrectly. See the other replies for the Prototypish way of doing loops. -Thomas Am 21.06.2006 um 00:21 schrieb Keith Davis:> function CreateTOC(){ > > var $aTOC = document.getElementsByClassName(''tocitem''); > > var $temp = ''''; > > if(isArray($aTOC)){ > > for($t in $aTOC){ > > $temp += ''t='' + $t + '', '' + $aTOC[$t].name > + ''<BR>''; > > } > > $(''test1'').innerHTML = $temp; > > } > > } > > > When I run this code as you see it, this is the result: > > t=indexOf, undefined > t=without, undefined > t=flatten, undefined > t=compact, undefined > t=last, undefined > t=first, undefined > t=clear, undefined > t=_each, undefined > t=_reverse, undefined > t=entries, undefined > t=member, undefined > t=select, undefined > t=find, undefined > t=map, undefined > t=inspect, undefined > t=zip, undefined > t=toArray, undefined > t=sortBy, undefined > t=reject, undefined > t=pluck, undefined > t=partition, undefined > t=min, undefined > t=max, undefined > t=invoke, undefined > t=inject, undefined > t=include, undefined > t=grep, undefined > t=findAll, undefined > t=detect, undefined > t=collect, undefined > t=any, undefined > t=all, undefined > t=each, undefined > t=0, #Assessments Instruments > t=1, chiefcomplaint > t=2, History of Present Injury > > The only tocitem''s are the last three in that list. Where is the > rest of that array coming from?_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs