473,396 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Associative with MultiDimensional arrays

Hi,

I'm trying to mix them up for something like this but firefox gives me
profiles.p1 is undefined error...

<html><body>

<script type="text\javascript">
function showVal()
{
alert(profiles["p1"][0].x);
}

function profileRecord(x, y, z)
{
this.x = x;
this.y = y;
this.z = z;
}

var profiles = new Array(3);

profiles["p1"][0] = new profileRecord(10, 20, 30);
profiles["p1"][1] = new profileRecord(10, 20, 30);
profiles["p1"][2] = new profileRecord(10, 20, 30);
</script>

<form>
<input type="button" onclick="showVal()" value="Show" />
</form>

</body></html>

I expect "10" would be displayed but instead I get the error that
profiles.p1 doesn't exist. How can I correct it?

Thanks
Sep 12 '06 #1
6 1114
VK
Aziz wrote:
profiles["p1"][0] = new profileRecord(10, 20, 30);
profiles["p1"][1] = new profileRecord(10, 20, 30);
profiles["p1"][2] = new profileRecord(10, 20, 30);
<snip>
alert(profiles["p1"][0].x);
is effectively
undefined[0] = new profileRecord(10, 20, 30);
alert(undefined[0].x)
this makes the problem more visual.
I expect "10" would be displayed but instead I get the error that
profiles.p1 doesn't exist. How can I correct it?
function showVal()
{
alert(profiles[1].x);
}

function profileRecord(x, y, z)
{
this.x = x;
this.y = y;
this.z = z;
}

var profiles = new Array(3);

profiles[0] = new profileRecord(10, 20, 30);
profiles[1] = new profileRecord(10, 20, 30);
profiles[2] = new profileRecord(10, 20, 30);

Sep 12 '06 #2
On 12 Sep 2006 09:38:57 -0700, "VK" <sc**********@yahoo.comwrote:
>Aziz wrote:
> profiles["p1"][0] = new profileRecord(10, 20, 30);
profiles["p1"][1] = new profileRecord(10, 20, 30);
profiles["p1"][2] = new profileRecord(10, 20, 30);
<snip>
> alert(profiles["p1"][0].x);

is effectively
undefined[0] = new profileRecord(10, 20, 30);
alert(undefined[0].x)
this makes the problem more visual.
>I expect "10" would be displayed but instead I get the error that
profiles.p1 doesn't exist. How can I correct it?

function showVal()
{
alert(profiles[1].x);
}

function profileRecord(x, y, z)
{
this.x = x;
this.y = y;
this.z = z;
}

var profiles = new Array(3);

profiles[0] = new profileRecord(10, 20, 30);
profiles[1] = new profileRecord(10, 20, 30);
profiles[2] = new profileRecord(10, 20, 30);
Thanks but what you suggest is a 2 dimensional array, while I try to
implement a 3 dimensional array with the first index as a string. Is
it not possible?
Sep 12 '06 #3
VK

Aziz wrote:
I try to implement a 3 dimensional array with the first index as a string. Is
it not possible?
Definitely not, because there are not "arrays with string index", they
do not exist in the nature. There objects of type Object with
properties and objects of type Array with integer indicies.

You can create an object and store array references as its properties:

var obj = {};

obj.p1 = [1,2,3];
obj.p2 = [1,2,3];

further more each array element can hold a reference either to a map or
an array:

obj.p1[0] = [1,2,3];
obj.p2[1] = {'foo':'bar'};

and further and further up to structures of any complexity.

Sep 12 '06 #4

Aziz wrote:
Hi,

I'm trying to mix them up for something like this but firefox gives me
profiles.p1 is undefined error...

<html><body>

<script type="text\javascript">
function showVal()
{
alert(profiles["p1"][0].x);
}

function profileRecord(x, y, z)
{
this.x = x;
this.y = y;
this.z = z;
}

var profiles = new Array(3);

profiles["p1"][0] = new profileRecord(10, 20, 30);
profiles["p1"] = [ new profileRecord(10, 20, 30) ];
profiles["p2"] = [ new profileRecord(10, 20, 30) ];
...
profiles["p1"][1] = new profileRecord(10, 20, 30);
profiles["p1"][2] = new profileRecord(10, 20, 30);
</script>

<form>
<input type="button" onclick="showVal()" value="Show" />
</form>

</body></html>

I expect "10" would be displayed but instead I get the error that
profiles.p1 doesn't exist. How can I correct it?
Do you intend to use any of profiles' array-ness? You appear to be
using it as a simple object, so:

profiles = new Object();
profiles["p1"] = [ new profileRecord(10, 20, 30) ];
profiles["p2"] = [ new profileRecord(10, 20, 30) ];
profiles["p3"] = [ new profileRecord(10, 20, 30) ];

or

profiles = {
p1 : [ new profileRecord(10, 20, 30) ],
p2 : [ new profileRecord(10, 20, 30) ],
p3 : [ new profileRecord(10, 20, 30) ]
}
Would be considered more approrpriate. I wouldn't call it a
multi-dimensional array, it's just a collection of named arrays that
each has a single element: an object with properties x, y and z.

A multi-dimensional array would have the property names as indexes of
an array:

profiles = [
[ new profileRecord(10, 20, 30) ],
[ new profileRecord(10, 20, 30) ],
[ new profileRecord(10, 20, 30) ]
]
Your profileRecord objects are just plain objects, you could also
create them using:

profiles = {
p1 : [ {x:10, y:20, z:30} ],
p2 : [ {x:10, y:20, z:30} ],
p3 : [ {x:10, y:20, z:30} ]
}

--
Rob

Sep 12 '06 #5
Thanks a lot...
On 12 Sep 2006 14:05:19 -0700, "RobG" <rg***@iinet.net.auwrote:
>
Aziz wrote:
>Hi,

I'm trying to mix them up for something like this but firefox gives me
profiles.p1 is undefined error...

<html><body>

<script type="text\javascript">
function showVal()
{
alert(profiles["p1"][0].x);
}

function profileRecord(x, y, z)
{
this.x = x;
this.y = y;
this.z = z;
}

var profiles = new Array(3);

profiles["p1"][0] = new profileRecord(10, 20, 30);

profiles["p1"] = [ new profileRecord(10, 20, 30) ];
profiles["p2"] = [ new profileRecord(10, 20, 30) ];
...
> profiles["p1"][1] = new profileRecord(10, 20, 30);
profiles["p1"][2] = new profileRecord(10, 20, 30);
</script>

<form>
<input type="button" onclick="showVal()" value="Show" />
</form>

</body></html>

I expect "10" would be displayed but instead I get the error that
profiles.p1 doesn't exist. How can I correct it?

Do you intend to use any of profiles' array-ness? You appear to be
using it as a simple object, so:

profiles = new Object();
profiles["p1"] = [ new profileRecord(10, 20, 30) ];
profiles["p2"] = [ new profileRecord(10, 20, 30) ];
profiles["p3"] = [ new profileRecord(10, 20, 30) ];

or

profiles = {
p1 : [ new profileRecord(10, 20, 30) ],
p2 : [ new profileRecord(10, 20, 30) ],
p3 : [ new profileRecord(10, 20, 30) ]
}
Would be considered more approrpriate. I wouldn't call it a
multi-dimensional array, it's just a collection of named arrays that
each has a single element: an object with properties x, y and z.

A multi-dimensional array would have the property names as indexes of
an array:

profiles = [
[ new profileRecord(10, 20, 30) ],
[ new profileRecord(10, 20, 30) ],
[ new profileRecord(10, 20, 30) ]
]
Your profileRecord objects are just plain objects, you could also
create them using:

profiles = {
p1 : [ {x:10, y:20, z:30} ],
p2 : [ {x:10, y:20, z:30} ],
p3 : [ {x:10, y:20, z:30} ]
}
Sep 12 '06 #6
Thanks a lot...

On 12 Sep 2006 13:56:38 -0700, "VK" <sc**********@yahoo.comwrote:
>
Aziz wrote:
>I try to implement a 3 dimensional array with the first index as a string. Is
it not possible?

Definitely not, because there are not "arrays with string index", they
do not exist in the nature. There objects of type Object with
properties and objects of type Array with integer indicies.

You can create an object and store array references as its properties:

var obj = {};

obj.p1 = [1,2,3];
obj.p2 = [1,2,3];

further more each array element can hold a reference either to a map or
an array:

obj.p1[0] = [1,2,3];
obj.p2[1] = {'foo':'bar'};

and further and further up to structures of any complexity.
Sep 12 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
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...
13
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
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
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...
4
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...
8
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...
9
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...
41
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...
11
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...
9
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.