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

M

mark4asp

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

Michael Winter

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
 
L

Lasse Reichstein Nielsen

mark4asp said:
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
 
M

mark4asp

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

mark4asp

mark4asp said:
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.
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top