multi-dem array

M

Michael Hill

If I have this array like:

var XXXX = new Array(
"('2003','45.24','.2',true,true)",
"('2004','45.75','.25')",
"('2005','46.01','.27')",
);

and have identified the array as "selectedArray" then this code below
iterates over the array
for ( var i=0; i < selectedArray.length; i++ )
{
var g = selectedArray;
alert(g);
}

selectedArray.length = 3, right?
if I look at g I get ('2006','39.45','.21'), but if I want to see the
individual elements shouldn't I be able to use (which dosent work):
var h = selectedArray[0]; to get '2006'
var i = selectedArray[1]; to get '39.45'
var j = selectedArray[2]; to get '.21'

Mike
 
L

Lasse Reichstein Nielsen

Michael Hill said:
If I have this array like:

var XXXX = new Array(
"('2003','45.24','.2',true,true)",
"('2004','45.75','.25')",
"('2005','46.01','.27')",
);

and have identified the array as "selectedArray" then this code below
iterates over the array
for ( var i=0; i < selectedArray.length; i++ )
{
var g = selectedArray;
alert(g);
}

selectedArray.length = 3, right?


At least 3, yes. You have an extra comma after the third entry, which
could potentially make some browsers give a length of 4. The standard
isn't so fuzzy, and any length of at least 3 is legal.
if I look at g I get ('2006','39.45','.21'), but if I want to see the
individual elements shouldn't I be able to use (which dosent work):
var h = selectedArray[0]; to get '2006'


No. selectedArray is a string, not an array.
If you want an array of arrays, you should write

var XXXX = new Array(
new Array("2003","45.24",".2",true,true),
new Array("2004","45.75",".25"),
new Array("2005","46.01",".27")
);

or the shorter form:
var XXXX = [["2003","45.24",".2",true,true],
["2004","45.75",".25"],
["2005","46.01",".27"]];


/L
 
L

Lasse Reichstein Nielsen

You have an extra comma after the third entry, which could
potentially make some browsers give a length of 4.

I'll take that back. It is a syntax error.
If you write arrays using array literal notation, then extra commas
are legal:
var X = [2,3,4,]; // legal
var Y = new Array(2,3,4,); // illegal syntax

/L
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top