473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1651
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: annoyingmouse2002 | last post by:
Hi there, sorry if this a long post but I'm really just starting out. I've been using MSXML to parse an OWL but would like to use a different solution. Basically it reads the OWL (Based on XML)...
13
by: Kevin | last post by:
Help! Why are none of these valid? var arrayName = new Array(); arrayName = new Array('alpha_val', 1); arrayName = ; I'm creating/writing the array on the server side from Perl, but I
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
1
by: Alfredo Magallón Arbizu | last post by:
Hello, I need to pass the contents of a dataset to the client in order to use these contents with javascript. What is the best way to achieve that? Normally I use controls like a grid or...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.