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. 5 1960
On Fri, 24 Sep 2004 22:09:44 +0100, mark4asp
<ma************ ****@ntlworld.c om> 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.
mark4asp <ma************ ****@ntlworld.c om> 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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
On Sat, 25 Sep 2004 10:48:15 GMT, "Michael Winter"
<M.******@bluey onder.co.invali d> 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.
On Sat, 25 Sep 2004 13:54:00 +0200, Lasse Reichstein Nielsen
<lr*@hotpop.com > wrote: mark4asp <ma************ ****@ntlworld.c om> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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";
|
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
|
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...
|
by: Derek Basch |
last post by:
Is there any way to associate name/value pairs during an array
initialization? Like so:
sType = "funFilter"
filterTypeInfo = ;
filterTypeInfo = new Array("type" : sType);
I can do it using this:
sType = "String"
|
by: VK |
last post by:
Or why I just did myArray = "Computers" but myArray.length is
showing 0. What a hey?
There is a new trend to treat arrays and hashes as they were some
variations of the same thing. But they are not at all.
If you are doing *array", then you have to use only integer values for
array index, as it was since ALGOL.
| |
by: Robert Mark Bram |
last post by:
Hi All!
How do you get the length of an associative array?
var my_cars= new Array()
my_cars="Mustang";
my_cars="Station Wagon";
my_cars="SUV";
alert(my_cars.length);
|
by: Angus Comber |
last post by:
Hello
I want to create a lookup table where I can store string keys:
For example:
192.168.0.1 -> Purple
192.168.0.2 -> Blue
192.168.0.3 -> Red
|
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.
|
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";
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |