for in and arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Thies

    for in and arrays

    I think I am misundertanding how "in" works.

    I thought I could do this:

    var div_array=docum ent.getElements ByTagName('div' );

    for(var el in div_array){
    // I thought el would be an element of that array
    // doing this: alert(el) yields either the elements ID or "length"
    // why is that?
    }

    This works, which I thought was the same.

    for(var i=0; i< div_array.lengt h; i++){
    var el=div_array[i];
    alert(el.innerH TML);
    }

    Cheers,
    Jeff
  • Jeff Thies

    #2
    Re: for in and arrays

    Well, I see what I did wrong. I shouldn't write javascript after being
    up late!

    Jeff[color=blue]
    >
    > I think I am misundertanding how "in" works.
    >
    > I thought I could do this:
    >
    > var div_array=docum ent.getElements ByTagName('div' );
    >
    > for(var el in div_array){
    > // I thought el would be an element of that array[/color]

    // should be: div_array[el]

    // which yields either the ID or it's position or the string "length"
    which still puzzles me!

    [color=blue]
    > // doing this: alert(el) yields either the elements ID or "length"
    > // why is that?
    > }
    >
    > This works, which I thought was the same.
    >
    > for(var i=0; i< div_array.lengt h; i++){
    > var el=div_array[i];
    > alert(el.innerH TML);
    > }
    >
    > Cheers,
    > Jeff[/color]

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: for in and arrays

      Jeff Thies <cyberjeff@spri ntmail.com> writes:
      [color=blue]
      > I thought I could do this:
      >
      > var div_array=docum ent.getElements ByTagName('div' );
      >
      > for(var el in div_array){
      > // I thought el would be an element of that array[/color]

      You caught this one. I do it all the time too. A bad habit from Perl :).
      [color=blue]
      > // doing this: alert(el) yields either the elements ID or "length"
      > // why is that?[/color]

      What
      for (id in obj) { ... }
      does is to let id iterate through the enumerable properties of the
      object obj.

      If obj is an array, the enumerable properties are usually only the
      ones with integer names (length, push, etc. are set to be
      non-enumerable), but you can add other properties that will be
      enumerable.

      That is
      var arr = [10,20,30];
      for (var i in arr) { ... } // i is in the set {0,1,2}

      But if you add another property, it is also enumerable.

      var arr = [10,20,30];
      arr["golfball"] = 40;
      for (var i in arr) { ... } // i is in the set {0,1,2,golfball }

      What bites you here is that the result of
      document.getEle mentsByTagName is a collection, not an array.
      Many of its properties are enumerable.

      [color=blue]
      > This works, which I thought was the same.
      >
      > for(var i=0; i< div_array.lengt h; i++){[/color]

      It is only the same, if the enumerable properties of div_array are
      exactly the interes from zero to length-1.

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...