471,119 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,119 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 7524
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Abdullah Kauchali | last post: by
6 posts views Thread by mark4asp | last post: by
4 posts views Thread by Robert | last post: by
9 posts views Thread by Charles Banas | last post: by
41 posts views Thread by Rene Nyffenegger | last post: by
11 posts views Thread by Bosconian | last post: by

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.