Associative with MultiDimensional arrays

A

Aziz

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
 
V

VK

Aziz said:
profiles["p1"][0] = new profileRecord(10, 20, 30);
profiles["p1"][1] = new profileRecord(10, 20, 30);
profiles["p1"][2] = new profileRecord(10, 20, 30);
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);
 
A

Aziz

Aziz said:
profiles["p1"][0] = new profileRecord(10, 20, 30);
profiles["p1"][1] = new profileRecord(10, 20, 30);
profiles["p1"][2] = new profileRecord(10, 20, 30);
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?
 
V

VK

Aziz said:
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.
 
R

RobG

Aziz said:
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} ]
}
 
A

Aziz

Thanks a lot...


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} ]
}
 
A

Aziz

Thanks a lot...

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.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top