469,902 Members | 1,815 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,902 developers. It's quick & easy.

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.getElementById('test2').innerHTML+=myarra y[unit[i].value];
and it says 5

so far so good.
but this is undefined:
document.getElementById('test2').innerHTML+=myarra y[17.00];
why ?
Jul 23 '05 #1
4 1478
Jc
meltedown wrote:
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.getElementById('test2').innerHTML+=myarra y[unit[i].value];
and it says 5

so far so good.
but this is undefined:
document.getElementById('test2').innerHTML+=myarra y[17.00];
why ?


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/

Jul 23 '05 #2
Jc wrote:
meltedown wrote:
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.getElementById('test2').innerHTML+=myar ray[unit[i].value];
and it says 5

so far so good.
but this is undefined:
document.getElementById('test2').innerHTML+=myar ray[17.00];
why ?

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/

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.
Jul 23 '05 #3
Jc
meltedown wrote:
Jc wrote:
meltedown wrote:
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.getElementById('test2').innerHTML+=myar ray[unit[i].value];
and it says 5

so far so good.
but this is undefined:
document.getElementById('test2').innerHTML+=myar ray[17.00];
why ?

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/

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.


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
"associative array". Refer to:
http://msdn.microsoft.com/library/de...eReference.asp
(Click Statements, and then for..in Statement).

Example:

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

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

alert(mapQuantity["17"]); // alerts 5

Jul 23 '05 #4
On 18/06/2005 05:45, Jc wrote:

[snip]
You are unintentionally setting a property directly on your array
object [...]
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]
By calling myarray[unit[i].value], [...]
Not call - evaluate. I'd reserve the former for when discussing about
function calls, not property accessors.
The problem is that an array index needs to be an integer (a Number
object),


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.
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by annoyingmouse2002 | last post: by
1 post views Thread by Alfredo Magallón Arbizu | last post: by
104 posts views Thread by Leszek | last post: by
41 posts views Thread by Rene Nyffenegger | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.