multi-dem array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Hill

    multi-dem array

    If I have this array like:

    var XXXX = new Array(
    "('2003','45.24 ','.2',true,tru e)",
    "('2004','45.75 ','.25')",
    "('2005','46.01 ','.27')",
    );

    and have identified the array as "selectedAr ray" then this code below
    iterates over the array
    for ( var i=0; i < selectedArray.l ength; i++ )
    {
    var g = selectedArray[i];
    alert(g);
    }

    selectedArray.l ength = 3, right?
    if I look at g I get ('2006','39.45' ,'.21'), but if I want to see the
    individual elements shouldn't I be able to use (which dosent work):
    var h = selectedArray[i][0]; to get '2006'
    var i = selectedArray[i][1]; to get '39.45'
    var j = selectedArray[i][2]; to get '.21'

    Mike

  • Lasse Reichstein Nielsen

    #2
    Re: multi-dem array

    Michael Hill <hillmw@ram.lmt as.lmco.com> writes:
    [color=blue]
    > If I have this array like:
    >
    > var XXXX = new Array(
    > "('2003','45.24 ','.2',true,tru e)",
    > "('2004','45.75 ','.25')",
    > "('2005','46.01 ','.27')",
    > );
    >
    > and have identified the array as "selectedAr ray" then this code below
    > iterates over the array
    > for ( var i=0; i < selectedArray.l ength; i++ )
    > {
    > var g = selectedArray[i];
    > alert(g);
    > }
    >
    > selectedArray.l ength = 3, right?[/color]

    At least 3, yes. You have an extra comma after the third entry, which
    could potentially make some browsers give a length of 4. The standard
    isn't so fuzzy, and any length of at least 3 is legal.
    [color=blue]
    > if I look at g I get ('2006','39.45' ,'.21'), but if I want to see the
    > individual elements shouldn't I be able to use (which dosent work):
    > var h = selectedArray[i][0]; to get '2006'[/color]

    No. selectedArray[i] is a string, not an array.
    If you want an array of arrays, you should write

    var XXXX = new Array(
    new Array("2003","4 5.24",".2",true ,true),
    new Array("2004","4 5.75",".25"),
    new Array("2005","4 6.01",".27")
    );

    or the shorter form:
    var XXXX = [["2003","45.24", ".2",true,t rue],
    ["2004","45.75", ".25"],
    ["2005","46.01", ".27"]];


    /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

    • Lasse Reichstein Nielsen

      #3
      Re: multi-dem array

      Lasse Reichstein Nielsen <lrn@hotpop.com > writes:
      [color=blue]
      > Michael Hill <hillmw@ram.lmt as.lmco.com> writes:
      >[color=green]
      > > var XXXX = new Array(
      > > "('2003','45.24 ','.2',true,tru e)",
      > > "('2004','45.75 ','.25')",
      > > "('2005','46.01 ','.27')",
      > > );[/color][/color]
      [color=blue]
      > You have an extra comma after the third entry, which could
      > potentially make some browsers give a length of 4.[/color]

      I'll take that back. It is a syntax error.
      If you write arrays using array literal notation, then extra commas
      are legal:
      var X = [2,3,4,]; // legal
      var Y = new Array(2,3,4,); // illegal syntax

      /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

      • Michael Hill

        #4
        Re: multi-dem array

        So
        "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
        news:d6eo3z3i.f sf@hotpop.com.. .[color=blue]
        > Michael Hill <hillmw@ram.lmt as.lmco.com> writes:
        >[/color]
        <snip>

        I like this shorter form
        [color=blue]
        >
        > or the shorter form:
        > var XXXX = [["2003","45.24", ".2",true,t rue],
        > ["2004","45.75", ".25"],
        > ["2005","46.01", ".27"]];
        >[/color]

        So, If i had selected "XXXX" from a listbox and was going to iterate shoving
        this first value into another listbox why wouldn't this work?

        for ( var i=0; i < selectedArray.l ength; i++ )
        {
        eval( "document.form. mylistbox.optio ns[i][0]=" + "new Option" +
        selectedArray[i][0] );
        }

        Mike[color=blue]
        >
        > /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.'[/color]


        Comment

        • Michael Hill

          #5
          Re: multi-dem array

          Lasse, Sind sir Deutsch?

          Still having problems. se below.

          Mike

          "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
          news:r833tnmz.f sf@hotpop.com.. .[color=blue]
          > "Michael Hill" <hillmw@charter .net> writes:
          >[color=green][color=darkred]
          > > > var XXXX = [["2003","45.24", ".2",true,t rue],
          > > > ["2004","45.75", ".25"],
          > > > ["2005","46.01", ".27"]];
          > > >[/color]
          > >
          > > So, If i had selected "XXXX" from a listbox and was going to iterate[/color][/color]
          shoving[color=blue][color=green]
          > > this first value into another listbox why wouldn't this work?
          > >
          > > for ( var i=0; i < selectedArray.l ength; i++ )
          > > {
          > > eval( "document.form. mylistbox.optio ns[i][0]=" + "new Option" +
          > > selectedArray[i][0] );[/color]
          >
          > First of all, you *never* need to use eval to access an object property.
          > You will probably never need eval at all. Using eval in inefficient
          > and makes errors harder to find.
          >
          > This should do it (I always add the forms and elements collections):
          >
          > document.forms['form'].elements['mylistbox'].options[i] =
          > new Option(selected Array[i][0]);
          >[/color]

          This must not be adding the "value" to the listbox.

          I'm adding the array to the listbox using:

          for ( var i=0; i < selectedArray.l ength; i++ )
          {
          document.forms['form'].elements['mylistbox'].options[i] = new
          Option(selected Array[i][0]);
          }

          When I click on the listbox

          onclick="docume nt.forms['form'].elements['mylistbox'].options[document.forms
          ['form'].elements['mylistbox'].selectedIndex].value;"

          I'm getting ""
          [color=blue]
          > You just have a "[0]" too many. The options collection is only
          > one-dimensional.
          >
          > /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.'[/color]


          Comment

          Working...