javascript arrays

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

    javascript arrays

    Theres something very basic about javascript arrays I'm missing.
    The value of unit[i].value is 17.00
    and value of the qty[i].value is 5

    and I put these values into an array: myarray[unit[i].value]=qty[i].value;
    I accessed the qty[i].value by putting it into <div id='test2>:
    document.getEle mentById('test2 ').innerHTML+=m yarray[unit[i].value];
    and it says 5

    so far so good.
    but this is undefined:
    document.getEle mentById('test2 ').innerHTML+=m yarray[17.00];
    why ?
  • Jc

    #2
    Re: javascript arrays

    meltedown wrote:[color=blue]
    > Theres something very basic about javascript arrays I'm missing.
    > The value of unit[i].value is 17.00
    > and value of the qty[i].value is 5
    >
    > and I put these values into an array: myarray[unit[i].value]=qty[i].value;
    > I accessed the qty[i].value by putting it into <div id='test2>:
    > document.getEle mentById('test2 ').innerHTML+=m yarray[unit[i].value];
    > and it says 5
    >
    > so far so good.
    > but this is undefined:
    > document.getEle mentById('test2 ').innerHTML+=m yarray[17.00];
    > why ?[/color]

    You are unintentionally setting a property directly on your array
    object using a second "feature" of the square bracket notation, instead
    of adding the data as an indexed item into your array.

    By calling myarray[unit[i].value], you are expecting the value property
    to return a number, which you want to use as an array index. However,
    it returns a string, which is ambiguous in this case, since one of two
    operations will occur, based on what the string contains as text.

    The problem is that an array index needs to be an integer (a Number
    object), but you are passing it a String object, which is sometimes
    being converted to a Number, and therefore being used as an array
    index, and sometimes it is kept as a string, and therefore being used
    as a property name for the array object. Refer to the following
    documentation for the value property:


    Note the part where it says "sValue: String that specifies or receives
    the value for the control. ". This means you were essentially executing
    myarray["17.00"], although you intended to execute myarray[17.00].

    To get your code working using an array index as you intend, you need
    to convert the string being returned from .value into a number before
    trying to use it as an array index. The simplest way to do this is the
    unary + operator, although parseInt will work too.

    Here's two examples using both syntaxes:

    Example A:
    myarray[+unit[i].value] = "foo";

    Refer to the following site for details on the unary + operator:


    Example B:
    myarray[parseInt(unit[i].value, 10)] = "foo";

    The second parameter to the parseInt call is required, refer to:


    If you are curious about the property setting feature of javascript's
    square bracket notation, you may want to read up on this shortcut for
    setting properties on any object (including an array):


    FYI: You may want to read through the FAQ for this group, from which I
    have been referring to several sections: http://www.jibbering.com/faq/

    Comment

    • meltedown

      #3
      Re: javascript arrays

      Jc wrote:[color=blue]
      > meltedown wrote:
      >[color=green]
      >>Theres something very basic about javascript arrays I'm missing.
      >>The value of unit[i].value is 17.00
      >>and value of the qty[i].value is 5
      >>
      >>and I put these values into an array: myarray[unit[i].value]=qty[i].value;
      >>I accessed the qty[i].value by putting it into <div id='test2>:
      >>document.getE lementById('tes t2').innerHTML+ =myarray[unit[i].value];
      >>and it says 5
      >>
      >>so far so good.
      >>but this is undefined:
      >>document.getE lementById('tes t2').innerHTML+ =myarray[17.00];
      >>why ?[/color]
      >
      >
      > You are unintentionally setting a property directly on your array
      > object using a second "feature" of the square bracket notation, instead
      > of adding the data as an indexed item into your array.
      >
      > By calling myarray[unit[i].value], you are expecting the value property
      > to return a number, which you want to use as an array index. However,
      > it returns a string, which is ambiguous in this case, since one of two
      > operations will occur, based on what the string contains as text.
      >
      > The problem is that an array index needs to be an integer (a Number
      > object), but you are passing it a String object, which is sometimes
      > being converted to a Number, and therefore being used as an array
      > index, and sometimes it is kept as a string, and therefore being used
      > as a property name for the array object. Refer to the following
      > documentation for the value property:
      > http://msdn.microsoft.com/library/de...es/value_1.asp
      >
      > Note the part where it says "sValue: String that specifies or receives
      > the value for the control. ". This means you were essentially executing
      > myarray["17.00"], although you intended to execute myarray[17.00].
      >
      > To get your code working using an array index as you intend, you need
      > to convert the string being returned from .value into a number before
      > trying to use it as an array index. The simplest way to do this is the
      > unary + operator, although parseInt will work too.
      >
      > Here's two examples using both syntaxes:
      >
      > Example A:
      > myarray[+unit[i].value] = "foo";
      >
      > Refer to the following site for details on the unary + operator:
      > http://www.jibbering.com/faq/faq_not....html#tcNumber
      >
      > Example B:
      > myarray[parseInt(unit[i].value, 10)] = "foo";
      >
      > The second parameter to the parseInt call is required, refer to:
      > http://www.jibbering.com/faq/#FAQ4_12
      >
      > If you are curious about the property setting feature of javascript's
      > square bracket notation, you may want to read up on this shortcut for
      > setting properties on any object (including an array):
      > http://www.jibbering.com/faq/faq_not..._brackets.html
      >
      > FYI: You may want to read through the FAQ for this group, from which I
      > have been referring to several sections: http://www.jibbering.com/faq/
      >[/color]
      Why is there no mention of the word 'array' on any of these pages ?
      I guess I'm just too frustrated to think. I just want to iterate through
      an associative array.

      Comment

      • Jc

        #4
        Re: javascript arrays

        meltedown wrote:[color=blue]
        > Jc wrote:[color=green]
        > > meltedown wrote:
        > >[color=darkred]
        > >>Theres something very basic about javascript arrays I'm missing.
        > >>The value of unit[i].value is 17.00
        > >>and value of the qty[i].value is 5
        > >>
        > >>and I put these values into an array: myarray[unit[i].value]=qty[i].value;
        > >>I accessed the qty[i].value by putting it into <div id='test2>:
        > >>document.getE lementById('tes t2').innerHTML+ =myarray[unit[i].value];
        > >>and it says 5
        > >>
        > >>so far so good.
        > >>but this is undefined:
        > >>document.getE lementById('tes t2').innerHTML+ =myarray[17.00];
        > >>why ?[/color]
        > >
        > >
        > > You are unintentionally setting a property directly on your array
        > > object using a second "feature" of the square bracket notation, instead
        > > of adding the data as an indexed item into your array.
        > >
        > > By calling myarray[unit[i].value], you are expecting the value property
        > > to return a number, which you want to use as an array index. However,
        > > it returns a string, which is ambiguous in this case, since one of two
        > > operations will occur, based on what the string contains as text.
        > >
        > > The problem is that an array index needs to be an integer (a Number
        > > object), but you are passing it a String object, which is sometimes
        > > being converted to a Number, and therefore being used as an array
        > > index, and sometimes it is kept as a string, and therefore being used
        > > as a property name for the array object. Refer to the following
        > > documentation for the value property:
        > > http://msdn.microsoft.com/library/de...es/value_1.asp
        > >
        > > Note the part where it says "sValue: String that specifies or receives
        > > the value for the control. ". This means you were essentially executing
        > > myarray["17.00"], although you intended to execute myarray[17.00].
        > >
        > > To get your code working using an array index as you intend, you need
        > > to convert the string being returned from .value into a number before
        > > trying to use it as an array index. The simplest way to do this is the
        > > unary + operator, although parseInt will work too.
        > >
        > > Here's two examples using both syntaxes:
        > >
        > > Example A:
        > > myarray[+unit[i].value] = "foo";
        > >
        > > Refer to the following site for details on the unary + operator:
        > > http://www.jibbering.com/faq/faq_not....html#tcNumber
        > >
        > > Example B:
        > > myarray[parseInt(unit[i].value, 10)] = "foo";
        > >
        > > The second parameter to the parseInt call is required, refer to:
        > > http://www.jibbering.com/faq/#FAQ4_12
        > >
        > > If you are curious about the property setting feature of javascript's
        > > square bracket notation, you may want to read up on this shortcut for
        > > setting properties on any object (including an array):
        > > http://www.jibbering.com/faq/faq_not..._brackets.html
        > >
        > > FYI: You may want to read through the FAQ for this group, from which I
        > > have been referring to several sections: http://www.jibbering.com/faq/
        > >[/color]
        > Why is there no mention of the word 'array' on any of these pages ?
        > I guess I'm just too frustrated to think. I just want to iterate through
        > an associative array.[/color]

        I assumed you wanted to use myarray as an indexed array, not an
        associative array. The reason the word array is not mentioned at
        http://www.jibbering.com/faq/faq_not..._brackets.html is because
        there really isn't such thing as an associative array in javascript, it
        is technically a property (along with its value) on an object, accessed
        through square bracket notation.

        So, now that I realize that you want to use an array as an associative
        array, you probably shouldn't be using an array in the first place.
        Just use an Object, then you don't have to worry about the array
        getting confused about whether you are trying to use it as an indexed
        or an associative array.

        This means that yes, passing .value directly into myarray using square
        bracket notation is correct (except myarray should be an object, not an
        array).

        For example, define myarray as follows (let's call it mapQuantity for
        clarity):

        var mapQuantity = new Object();

        Now, we can use this as an associative array (or map) using the square
        bracket notation (as described in the FAQ):

        mapQuantity["17"] = 5; // eg. mapQuantity[unit[i].value]
        mapQuantity["8.00"] = 3;

        If we had been using an Array, the "17" would have been converted into
        a number and the array index 17 would get set to 5, which isn't
        desired. This is why an Object is being used instead.

        The for..in statement is used to loop through the values in this
        "associativ e array". Refer to:

        (Click Statements, and then for..in Statement).

        Example:

        for (var keyPrice in mapQuantity) {
        alert("Key (Price) ["+keyPrice+ "]");
        alert("Value (Quantity) ["+mapQuanti ty[keyPrice]+"]");
        } //for

        Note that you can also get the value of a key (which has to be a string
        BTW) as follows:

        alert(mapQuanti ty["17"]); // alerts 5

        Comment

        • Michael Winter

          #5
          Re: javascript arrays

          On 18/06/2005 05:45, Jc wrote:

          [snip]
          [color=blue]
          > You are unintentionally setting a property directly on your array
          > object [...][/color]

          It may not be all that unintentional, as I'd expect the OP to want
          prices of 17.00 and 17.50, for example, to be listed separately. If this
          is the case, then the OP needs to implement a hash table, and do away
          with native objects altogether.

          [snip]
          [color=blue]
          > By calling myarray[unit[i].value], [...][/color]

          Not call - evaluate. I'd reserve the former for when discussing about
          function calls, not property accessors.
          [color=blue]
          > The problem is that an array index needs to be an integer (a Number
          > object),[/color]

          Perhaps you have read this by now, but that statement is false. Property
          accessors, which includes array indicies, are always strings. After
          evaluating an expression within square brackets, the result is coerced
          to a string value.

          An Array object is special in that has a modified internal [[Put]]
          method. This method attempts to convert the property name into an
          unsigned 32-bit integer (using the internal ToUint32 operator), then
          back into a string. If this treated string matches the original, then
          the property name is an array index, and treated as such. If not, it is
          an object property.

          In the original post, the OP effectively has:

          myarray['17.00']

          and

          myarray[17.00]

          In the first case, the transition will proceed as follows:

          ToString('17.00 ') -> '17.00'
          ToUint32('17.00 ') -> 17
          ToString(17) -> '17'

          The two strings are different, so it is an object property.

          In the second case:

          ToString(17.00) -> '17'
          ToUint32('17') -> 17
          ToString(17) -> '17'

          The two strings are the same, so it is an array index. Notice that if
          the second case was:

          myarray['17']

          it would still be considered to be an array index.

          [snip]

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply by e-mail.

          Comment

          Working...