472,952 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Associative multidimensional arrays

Is there any way to associate name/value pairs during an array
initialization? Like so:

sType = "funFilter"
filterTypeInfo = [];
filterTypeInfo[sType] = new Array("type" : sType);

I can do it using this:

sType = "String"
filterTypeInfo = [];
filterTypeInfo[sType] = [];
filterTypeInfo[sType]["type"] = sType;

but that seems rather cludgy. I want to use array objects not Object
objects as described here:

http://www.crockford.com/JSON/index.html

Thanks,
Derek Basch

Jul 23 '05 #1
8 7626
"Derek Basch" <db****@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Is there any way to associate name/value pairs during an array
initialization? Like so:

sType = "funFilter"
filterTypeInfo = [];
filterTypeInfo[sType] = new Array("type" : sType);

I can do it using this:

sType = "String"
filterTypeInfo = [];
filterTypeInfo[sType] = [];
filterTypeInfo[sType]["type"] = sType;

but that seems rather cludgy. I want to use array objects not Object
objects as described here:

http://www.crockford.com/JSON/index.html

Thanks,
Derek Basch


Have you looked at the Dictionary object?

http://www.w3schools.com/asp/asp_ref_dictionary.asp
Jul 23 '05 #2
Lee
Derek Basch said:

Is there any way to associate name/value pairs during an array
initialization? Like so:

sType = "funFilter"
filterTypeInfo = [];
filterTypeInfo[sType] = new Array("type" : sType);

I can do it using this:

sType = "String"
filterTypeInfo = [];
filterTypeInfo[sType] = [];
filterTypeInfo[sType]["type"] = sType;

but that seems rather cludgy. I want to use array objects not Object
objects as described here:


"associative arrays" in Javascript are *always* "Object objects".
If the object happens to be an Array, that's just coincidental,
because the Array attributes are ignored.

Jul 23 '05 #3
Derek Basch wrote:
but that seems rather cludgy. I want to use array objects not Object
objects<snip>


As Derek said:

http://www.quirksmode.org/js/associative.html

So we have to use one of JavaScript's minor mysteries. In JavaScript,
objects are also associative arrays (or hashes). That is, the property

theStatus.Home

can also be read or written by calling

theStatus['Home']
Jul 23 '05 #4
mscir wrote:
<snip>
So we have to use one of JavaScript's minor mysteries.
Minor mystery or standard language construct?
In JavaScript,
objects are also associative arrays (or hashes).
Objects are not necessarily either associative arrays or hashes (in the
senses that the terms may be used in other languages). Objects may be
implemented using associative arrays or hashes in whatever language the
implementation is written in but the language specification is not
interested in how its requirements are implemented. Objects are only
required to be _dynamic_ associations of named properties with values.
And indeed evidence suggests that the IE/JScript object implementation
is a list of some sort.

The important detail is that ECMAScript has no Array accessing syntax at
all, only property accessors (one type of which resemble array accessing
syntax from other languages). And that ECMAScript Array objects are just
Objects that implement additional behaviour when properties are accessed
(when the property names sufficiently resemble 32 bit unsigned integers)
and have additional methods defined on a custom Array.prototype.

Speaking of ECMAScript objects in terms of "associative arrays" and
"hashes" tends to obscure the real nature of those objects and lead to
misconceptions about what behaviour can be expected of ECMAScript
objects.
That is, the property

theStatus.Home

can also be read or written by calling

theStatus['Home']


And even when the - theStatus - identifier is a reference to an object
that is an Array object the array-ness of that object is irrelevant to
the use of either of the above property accessors.

Richard.
Jul 23 '05 #5
> Is there any way to associate name/value pairs during an array
initialization? Like so:

sType = "funFilter"
filterTypeInfo = [];
filterTypeInfo[sType] = new Array("type" : sType);
filterTypeInfo = {}; /* Note the braces, not brackets */
filterTypeInfo[sType] = {type : sType};

or

filterTypeInfor.funFilter = {type : sType};
I can do it using this:

sType = "String"
filterTypeInfo = [];
filterTypeInfo[sType] = [];
filterTypeInfo[sType]["type"] = sType;

but that seems rather cludgy.
Right you are.
I want to use array objects not Object
objects as described here:

http://www.crockford.com/JSON/index.html


Why do you want to do it incorrectly?
It is correct to use objects here, not arrays.
Jul 23 '05 #6
rh
Richard Cornford wrote:
mscir wrote:
<snip>
So we have to use one of JavaScript's minor mysteries.
Minor mystery or standard language construct?
In JavaScript,
objects are also associative arrays (or hashes).


Objects are not necessarily either associative arrays or hashes (in

the senses that the terms may be used in other languages). Objects may be
implemented using associative arrays or hashes in whatever language the implementation is written in but the language specification is not
interested in how its requirements are implemented. Objects are only
required to be _dynamic_ associations of named properties with values. And indeed evidence suggests that the IE/JScript object implementation is a list of some sort.

The important detail is that ECMAScript has no Array accessing syntax at all, only property accessors (one type of which resemble array accessing syntax from other languages). And that ECMAScript Array objects are just Objects that implement additional behaviour when properties are accessed (when the property names sufficiently resemble 32 bit unsigned integers) and have additional methods defined on a custom Array.prototype.

Speaking of ECMAScript objects in terms of "associative arrays" and
"hashes" tends to obscure the real nature of those objects and lead to misconceptions about what behaviour can be expected of ECMAScript
objects.


As you correctly note, there is often a problem of misconception that
arises when the term "associative array" chances into the vicinity of
ECMAScript. However, in my view, the "real nature of objects" gets
completely obscured when their relationship to "associative arrays" is
tossed.

The "real nature" of ECMAScript Object objects is that they are simply
enhanced associative arrays. The enhancement comes through creation of
a linkage (the prototype chain) of underlying associative arrays. In
the case of the fundamental Object object, the chain is wonderfully
short, and the ramifications upon key-based lookup entirely explainable
and readily comprehensible.

While you have provided all kinds of very valuable information in the
above, it seems to me that anyone who is not fully familiar with
ECMAScript objects characteristics would think that they are totally
foreign and full of wonderful, mysterious construction, and of which
the uninitiated are yet become fully cognizant. And that just
constitutes a different form of obscuration, which I would quite truly
think would not be your intent.

Regards,

../rh

Jul 23 '05 #7
Thanks for the thorough answer Richard. I had to read it about 20 times
but I get it now.

Given an array:

family_names = new Array("Bob", "Judy");

I can assign a property to it like this:

family_names['daughter'] = 'Helga';

because it is also an Object object.

However, the 'daughter' property can't be accessed using 32 bit
unsigned integers because it wasnt added to the Array object as an
Array property but as an Object property. In other words:

alert(family_names[0]);

will return 'undefined'.

alert(family_names['daughter']);

will return 'Helga'.

alert(family_names['0']);

will return 'Bob'
Further, this code:

for (i in family_names)
{
alert(i);
}

returns '0', '1' and 'daughter'. So, apparently the 32 bit unsigned
integers are properties of the object but the property value can't be
accessed with the dotted notation that you could use to access the
'daughter' property:

family_names.0 = Error
family_names.daughter = Helga

Interesting stuff. Feel free to correct any errors in my above logic.

Jul 23 '05 #8
Derek Basch wrote:
<snip>
Given an array:

family_names = new Array("Bob", "Judy");

I can assign a property to it like this:

family_names['daughter'] = 'Helga';

because it is also an Object object.

However, the 'daughter' property can't be accessed
using 32 bit unsigned integers because it wasnt added
to the Array object as an Array property but as an
Object property.
The "daughter" property of the Array object cannot be accessed as a 32
bit unsigned integer because the character sequence "daughter" in no way
resembles a 32 bit unsigned integer.
In other words:

alert(family_names[0]);

will return 'undefined'.
It will return "Bob". the number zero is type converted to the string
"0", which is the property name under which the string "Bob" is stored.
alert(family_names['daughter']);

will return 'Helga'.

alert(family_names['0']);

will return 'Bob'
And the - length - property of the Array remains 2, regardless of the
additional enumerable property of the Array Object.
Further, this code:

for (i in family_names)
{
alert(i);
}

returns '0', '1' and 'daughter'. So, apparently the
32 bit unsigned integers are properties of the object
but the property value can't be accessed with the dotted
notation that you could use to access the
'daughter' property:
Property names used in dot notation property accessors are required to
conform to the ECMAScript production rules for an Identifier; they may
not start with a decimal digit. Representations of 32 bit unsigned
integers start with a decimal digit and so cannot be identifiers.
Bracket notation does not place any limitations on the character
sequence used as the property name.
family_names.0 = Error
But it is a syntax error not a runtime error.
family_names.daughter = Helga

<snip>

Richard.
Jul 23 '05 #9

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

Similar topics

5
by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would...
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
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...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
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...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.