473,396 Members | 1,836 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,396 software developers and data experts.

Q: JScript associative array

Platform: Win2K/SP4

I am trying to use an associative array in JScript, but I am seeing
something weird. Example:

ArrayTest();

///////////////////////////////////////////////////
function ArrayTest()
{
var oArray = new Array( 0 );
dumpArrayKeys( oArray );

++oArray.length;
oArray["first"] = 0;
dumpArrayKeys( oArray );

++oArray.length;
oArray["second"] = 1;
dumpArrayKeys( oArray );
}
///////////////////////////////////////////////////
function dumpArrayKeys( oArray )
{
oLog.Log( "arrayLen: " + oArray.length );
for( var key in oArray )
{
oLog.Log( "key: " + key );
}
Log( "-----------------------------" );
}
///////////////////////////////////////////////////

The output I'm seeing:

arrayLen: 0
key: getKey
-----------------------------
arrayLen: 1
key: getKey
key: first
-----------------------------
arrayLen: 2
key: getKey
key: first
key: second
-----------------------------

I don't understand where the key value "getKey" is coming from or
what it represents. Notice that the key is there as soon as the Array
has been created, and it never goes away.

Thoughts?

Jul 23 '05 #1
2 1611
> I am trying to use an associative array in JScript, but I am seeing
something weird.


We see stuff like this all the time. In JavaScript, do not use an array
when you should be using an object. The rule is really simple:

If the subscripts are small integers, use an array.
Otherwise, use an object.

See http://www.crockford.com/javascript/survey.html
Jul 23 '05 #2
On 25 Jan 2005 18:19:14 -0800, bu********@yahoo.com wrote:
Platform: Win2K/SP4

I am trying to use an associative array in JScript, but I am seeing
something weird. Example:

ArrayTest();

///////////////////////////////////////////////////
function ArrayTest()
{
var oArray = new Array( 0 );
dumpArrayKeys( oArray );

++oArray.length;
oArray["first"] = 0;
dumpArrayKeys( oArray );

++oArray.length;
oArray["second"] = 1;
dumpArrayKeys( oArray );
}
///////////////////////////////////////////////////
function dumpArrayKeys( oArray )
{
oLog.Log( "arrayLen: " + oArray.length );
for( var key in oArray )
{
oLog.Log( "key: " + key );
}
Log( "-----------------------------" );
}
///////////////////////////////////////////////////

The output I'm seeing:

arrayLen: 0
key: getKey
-----------------------------
arrayLen: 1
key: getKey
key: first
-----------------------------
arrayLen: 2
key: getKey
key: first
key: second
-----------------------------

I don't understand where the key value "getKey" is coming from or
what it represents. Notice that the key is there as soon as the Array
has been created, and it never goes away.

Thoughts?


That code (once the errors are fixed) doesn't result in that output.
You've probably got more code that you didn't post where you added a
getKey method to the Array prototype, e.g.
Array.prototype.getKey = something;

Regards,
Steve
--
Please post questions to the newsgroup; everyone benefits.
This posting is provided "AS IS" with no warranties, and confers no rights.
Sample code subject to http://www.microsoft.com/info/cpyright.htm
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stefan Richter | last post by:
Hi, I want to create an associative Array with a PHP variable (article ID) as Key and another associative array as it's value. How do I instanciate it, how can I fill it? I want something...
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
6
by: RFS666 | last post by:
Hello, After I posted yesterday "using C# class in jscript", I have a new problem: I have a C# class - DBResult - that contains (and other variables) a string array (and other variables), that...
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...
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.