By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

Can I create a 2-D associative array - if so how?

P: n/a
Suppose I wanted to create an array that was associative in 2
dimensions. The rows are associated with numbers. The columns with
words.

For instance for 3 columns. This array will have 20 rows of three
columns and the words that I want to associate with the columns are
'Small', 'Medium' and 'Large'. All the array elements are positive
integers. What's the easiest way to do this?

The first 3 columns are shown below for the array that is associative
by row:

sizes = {1:[16, 17, 19], 2:[16, 18, 23], 3:[16, 18, 25] }

This is how I can do it for columns:

function sizes(Small, Medium, Large) {
this.Small = Small;
this.Medium = Medium
this.Large = Large;
}

r1 = new sizes(16, 19, 17);
r2 = new sizes(16, 23, 18);
r3 = new sizes(16, 25, 18);

The problem with that is that the rows are now indexed via a
zero-based index instead of the code I wanted to use (as in the first
definition of sizes above) - accessed via an index starting at 1.

Is there a way I could create an associative array to refer to data
item 25 in the array as:

sizes[3][Large]

PS: As always this is an illustrative example.

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Fri, 24 Sep 2004 22:09:44 +0100, mark4asp
<ma****************@ntlworld.com> wrote:
Suppose I wanted to create an array that was associative in 2
dimensions. The rows are associated with numbers. The columns with words.
Just to correct any misconceptions...

There is no such thing as an associative array in Javascript. You can
emulate them with the properties of an object, but they are not the same
thing.
For instance for 3 columns. This array will have 20 rows of three
columns and the words that I want to associate with the columns are
'Small', 'Medium' and 'Large'. All the array elements are positive
integers. What's the easiest way to do this?

The first 3 columns are shown below for the array that is associative by
row:

sizes = {1:[16, 17, 19], 2:[16, 18, 23], 3:[16, 18, 25] }


sizes = {
small : [16, 17, 19],
medium : [16, 18, 23],
large : [16, 18, 25]
};

sizes['large'][1] // 18
sizes.medium[0] // 16

var col = 'small';
sizes[col][2] // 19

[snip]

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

P: n/a
mark4asp <ma****************@ntlworld.com> writes:
Suppose I wanted to create an array that was associative in 2
dimensions. The rows are associated with numbers. The columns with
words. .... sizes = {1:[16, 17, 19], 2:[16, 18, 23], 3:[16, 18, 25] } .... function sizes(Small, Medium, Large) {
I would capitalize "sizes" to show that it is used as a constructor
function.
this.Small = Small;
this.Medium = Medium
this.Large = Large;
}
.... The problem with that is that the rows are now indexed via a
zero-based index instead of the code I wanted to use (as in the first
definition of sizes above) - accessed via an index starting at 1.
Try
var sizes = {1: new Sizes(16, 19, 17),
2: new Sizes(16, 23, 18),
3: new Sizes(16, 25, 18)}
Is there a way I could create an associative array to refer to data
item 25 in the array as:

sizes[3][Large]


'ere you go.

When you don't use the "arrayness" of the first coordinate (which basically
means that you don't use the "length" property or any method on it that
comes from Array.prototype), then you can just as well use a plain object.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3

P: n/a
Lasse Reichstein Nielsen <lr*@hotpop.com> writes:
sizes[3][Large]


'ere you go.


Ack. That should be
sizes[3]['Large']
or
sizes[3].Large

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #4

P: n/a
On Sat, 25 Sep 2004 10:48:15 GMT, "Michael Winter"
<M.******@blueyonder.co.invalid> wrote:
On Fri, 24 Sep 2004 22:09:44 +0100, mark4asp
<ma****************@ntlworld.com> wrote:
Suppose I wanted to create an array that was associative in 2
dimensions. The rows are associated with numbers. The columns with words.


Just to correct any misconceptions...

There is no such thing as an associative array in Javascript. You can
emulate them with the properties of an object, but they are not the same
thing.
For instance for 3 columns. This array will have 20 rows of three
columns and the words that I want to associate with the columns are
'Small', 'Medium' and 'Large'. All the array elements are positive
integers. What's the easiest way to do this?

The first 3 columns are shown below for the array that is associative by
row:

sizes = {1:[16, 17, 19], 2:[16, 18, 23], 3:[16, 18, 25] }


sizes = {
small : [16, 17, 19],
medium : [16, 18, 23],
large : [16, 18, 25]
};

sizes['large'][1] // 18
sizes.medium[0] // 16

var col = 'small';
sizes[col][2] // 19

[snip]

Hope that helps,
Mike


Thanks Mike and Lasse I'll remember this but I don't need it now as
I'm changing all this client-side data manipulation to server-side
code as previously recommended [due to the difficulty of setting up
meaningful data structures which emulate the complexity and ease of
use of a RDBS in client-side code (30K of data!)]. So much as I enjoy
using it, I won't be able to use javascript and must change to
something like c# or vb.net.

Jul 23 '05 #5

P: n/a
On Sat, 25 Sep 2004 13:54:00 +0200, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
mark4asp <ma****************@ntlworld.com> writes:
Suppose I wanted to create an array that was associative in 2
dimensions. The rows are associated with numbers. The columns with
words....
sizes = {1:[16, 17, 19], 2:[16, 18, 23], 3:[16, 18, 25] }

...
function sizes(Small, Medium, Large) {


I would capitalize "sizes" to show that it is used as a constructor
function.
this.Small = Small;
this.Medium = Medium
this.Large = Large;
}

...
The problem with that is that the rows are now indexed via a
zero-based index instead of the code I wanted to use (as in the first
definition of sizes above) - accessed via an index starting at 1.


Try
var sizes = {1: new Sizes(16, 19, 17),
2: new Sizes(16, 23, 18),
3: new Sizes(16, 25, 18)}
Is there a way I could create an associative array to refer to data
item 25 in the array as:

sizes[3][Large]


Thanks, this was exactly what I wanted. (My row references don't start
at zero and there are some gaps in them later. So I really need to
look up the data according to some associative method). [Or at least I
did - see my other post]

But I'm sure this info you've given me will be useful in future, for
other things.

PS: There are so few good javascript references available. Most take
the attitude that javascript will only ever be used for client side
scripting of the DOM. My 2 big javascript books (Wrox & Goodman) don't
even mention this kind of thing. The Netscape & MS online books don't
help much, nor did the old MSDN I had installed.
'ere you go.

When you don't use the "arrayness" of the first coordinate (which basically
means that you don't use the "length" property or any method on it that
comes from Array.prototype), then you can just as well use a plain object.

/L


Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.