MyArray.length returns undefined...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dag Sunde

    #1

    MyArray.length returns undefined...

    ....while MyArray.toStrin g() returns the expected value...

    I have a function that returns an array:

    function getProducts( auctionID )
    {
    var prodCount = 0;
    var prodArray = new Array()
    for( i=0; i < products.length ; i++ )
    {
    if( products[i][0] == auctionID )
    {
    prodArray[prodCount] = new Array(1);
    prodArray[prodCount][0] = products[i][1];
    prodArray[prodCount][1] = products[i][2];
    prodCount++;
    }
    }
    return prodArray;
    }

    and I use it here:

    var productArray
    productArray = getProducts(idx Auction);

    // now, productArray should hold the array returned from
    // my function above.
    var numProducts = productArray.le nght;
    var topInfo = document.getEle mentById("topIn foLayer");

    topInfo.innerHT ML = numProducts;
    ...

    This prints "undefined" in my topinfo layer???

    Anyone?

    TIA...

    --
    Dag.
    EMail: echo lvjnugnwewn | tr "vjenlgwu" "s@.odinr"

    "Anyone who cannot cope with mathematics is not fully human.
    At best he is a tolerable subhuman who has learned to wear
    shoes, bathe and not make messes in the house."
    --Lazarus Long, "Time Enough for Love", (Robert A. Heinlein)


  • George Jempty

    #2
    Re: MyArray.length returns undefined...

    Dag Sunde wrote:
    [color=blue]
    > ...while MyArray.toStrin g() returns the expected value...
    >
    > I have a function that returns an array:
    >
    > function getProducts( auctionID )
    > {
    > var prodCount = 0;
    > var prodArray = new Array()
    > for( i=0; i < products.length ; i++ )
    > {
    > if( products[i][0] == auctionID )
    > {
    > prodArray[prodCount] = new Array(1);
    > prodArray[prodCount][0] = products[i][1];
    > prodArray[prodCount][1] = products[i][2];
    > prodCount++;
    > }
    > }
    > return prodArray;
    > }
    >
    > and I use it here:
    >
    > var productArray
    > productArray = getProducts(idx Auction);
    >
    > // now, productArray should hold the array returned from
    > // my function above.
    > var numProducts = productArray.le nght;[/color]

    could be the above typo. it should be productArray.le ngth.

    however I don't think this is it anyway. Javascript "object" arrays, or
    "associativ e" arrays -- the ones WITHOUT numeric indexes: don't support
    the length property.

    Comment

    • Dag Sunde

      #3
      Re: MyArray.length returns undefined...

      "George Jempty" <fleetwoodd@bel lsouth.net> wrote in message
      news:n1%0c.967$ rB4.917@bignews 6.bellsouth.net ...[color=blue]
      > Dag Sunde wrote:
      >[color=green]
      > > ...while MyArray.toStrin g() returns the expected value...
      > >
      > > I have a function that returns an array:
      > >[/color][/color]

      <snipped/
      [color=blue][color=green]
      > >
      > > // now, productArray should hold the array returned from
      > > // my function above.
      > > var numProducts = productArray.le nght;[/color]
      >
      > could be the above typo. it should be productArray.le ngth.
      >
      > however I don't think this is it anyway. Javascript "object" arrays, or
      > "associativ e" arrays -- the ones WITHOUT numeric indexes: don't support
      > the length property.[/color]

      It was the typo (Damn, I *have* to find those glasses)!

      Thanks a lot, George!

      --
      Dag.


      Comment

      • Michael Winter

        #4
        Re: MyArray.length returns undefined...

        On Tue, 02 Mar 2004 10:43:15 GMT, Dag Sunde <ds.nope@orion. no.way> wrote:
        [color=blue]
        > ...while MyArray.toStrin g() returns the expected value...
        >
        > I have a function that returns an array:
        >
        > function getProducts( auctionID )
        > {
        > var prodCount = 0;
        > var prodArray = new Array()[/color]

        You forgot the terminating semi-colon (not strictly necessary, but good
        practice).
        [color=blue]
        > for( i=0; i < products.length ; i++ )[/color]

        You should declare 'i' local: use the var keyword.
        [color=blue]
        > {
        > if( products[i][0] == auctionID )
        > {
        > prodArray[prodCount] = new Array(1);[/color]

        You might as well create an empty array or, if you must, at least create
        it with the correct initial size (2).
        [color=blue]
        > prodArray[prodCount][0] = products[i][1];
        > prodArray[prodCount][1] = products[i][2];
        > prodCount++;
        > }
        > }
        > return prodArray;
        > }
        >
        > and I use it here:
        >
        > var productArray
        > productArray = getProducts(idx Auction);[/color]

        This *will* return an array, as long as no errors occur during execution
        of getProducts().
        [color=blue]
        > // now, productArray should hold the array returned from
        > // my function above.
        > var numProducts = productArray.le nght;[/color]

        This appears to be an obvious typo, but the only thing that appears to
        cause a problem. If there are no typos in your actual code (they may not
        actually appear as errors!), you'll have to post it verbatim.
        [color=blue]
        > var topInfo = document.getEle mentById("topIn foLayer");[/color]

        Do you check for support of the getElementById method? That is, does this
        appear anywhere?

        if( document.getEle mentById ) {
        // getElementById supported
        [color=blue]
        > topInfo.innerHT ML = numProducts;
        > ...[/color]

        Though you use arrays a lot here, you should not forget that JavaScript is
        an object-oriented language, and some of your arrays, such as those that
        describe products, might be better represented as objects.

        You can read about objects in Netscape's JavaScript Guide (watch for URL
        wrap):


        Douglas Crockford also has some things to say about objects, amongst other
        things about the JavaScript language:


        Mike


        Netscape's JavaScript Guide, v1.5:


        Douglas Crockford's website:


        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        Working...