473,769 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Associative multidimensiona l 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 7696
"Derek Basch" <db****@yahoo.c om> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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"
filterTypeIn fo = [];
filterTypeIn fo[sType] = new Array("type" : sType);

I can do it using this:

sType = "String"
filterTypeIn fo = [];
filterTypeIn fo[sType] = [];
filterTypeIn fo[sType]["type"] = sType;

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


"associativ e 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 "associativ e 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 "associativ e 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 "associativ e array" chances into the vicinity of
ECMAScript. However, in my view, the "real nature of objects" gets
completely obscured when their relationship to "associativ e 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_na mes[0]);

will return 'undefined'.

alert(family_na mes['daughter']);

will return 'Helga'.

alert(family_na mes['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.da ughter = 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_na mes[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_na mes['daughter']);

will return 'Helga'.

alert(family_na mes['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.da ughter = 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
6760
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 clearly be the most efficient way to do it, but it only works on a copy of the original array and not the original (which is counter intuitive in my estimation). Using each doesn't work consistently either. Not only that, it's unduly complex for...
13
2532
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
11208
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
2019
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 between London and the previous element is 25 (40-15), London has a 25% chance of being selected. When I call the function getAssocItem to do this I need to send in 2 arguments. Is there a quick way to get the maximum key value in the associative
4
2690
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 strings, not just sequential integers in a fixed range. www.sunsite.ualberta.ca/Documentation/Gnu/gawk-3.1.0/html_chapter/gawk_20.html (n.) A collection of data (an array) where individual items can be indexed (accessed) by a string, rather than by...
9
6673
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 function (which was translated from Fortran code), among other heinous and numerous declarations, is this bit: static float bbuff; static int bkey; static int buse;
41
4976
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 the hash are alphabetically sorted if the key happens to be alpha numeric. Which I believe makes sense because it allows for fast lookup of a key.
11
2963
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 Array(); teams = "Lakers";
9
4502
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 function: x = new int;
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10199
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9979
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7393
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5433
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3948
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.