Associative multidimensional arrays

D

Derek Basch

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
 
L

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.
 
M

McKirahan

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:

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
 
R

Richard Cornford

mscir wrote:
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.
 
D

Douglas Crockford

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.
 
R

rh

Richard said:
mscir wrote:


Minor mystery or standard language construct?


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
 
D

Derek Basch

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.
 
R

Richard Cornford

Derek Basch wrote:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top