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

Associative with MultiDimensional arrays

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.