JavaScript Array Funny

Soldato
Joined
18 Oct 2002
Posts
16,030
Location
The land of milk & beans
I'm trying to understand exactly what's going on here, but can't get my head around it...

Code:
	var arrAddresses = new Array()
			
	arrAddresses[0,0] = '23 Any Lane'
	arrAddresses[0,1] = 'Any Town'
	arrAddresses[0,2] = 'Anywhere'
	arrAddresses[0,3] = ''
	arrAddresses[0,4] = ''
	arrAddresses[0,5] = 'AN1 3ER'
	arrAddresses[0,6] = 'GB'
	arrAddresses[0,7] = '0207 878787'
	
	arrAddresses[1,0] = '25 Some Street'
	arrAddresses[1,1] = 'Some Town'
	arrAddresses[1,2] = 'Somewhere'
	arrAddresses[1,3] = ''
	arrAddresses[1,4] = ''
	arrAddresses[1,5] = 'SO4 3WH'
	arrAddresses[1,6] = 'GB'
	arrAddresses[1,7] = '0208 272727'
	
	arrAddresses[2,0] = 'Lorem Ipsum'
	arrAddresses[2,1] = 'Dolor sit'
	arrAddresses[2,2] = 'Amet consectetuer'
	arrAddresses[2,3] = 'Adipiscing'
	arrAddresses[2,4] = 'Elit'
	arrAddresses[2,5] = 'AB12 3CD'
	arrAddresses[2,6] = 'GB'
	arrAddresses[2,7] = '01234 567890'
	
	function setAddress(szSelectedAddress) {				
		alert(szSelectedAddress)
		
		alert(arrAddresses[szSelectedAddress,1])
	}
Now, no matter what I change the first index of the array to - even if it's hard coded to 1,1 - it always comes back with the last element's details (thats all the Latin stuff).

Anyone know why? I have a feeling it's a numpty mistake, but I've been staring at it so long I can't see it :o
 
Your array hasn't been correctly initialised, you have only initialised a single dimensional array, Javascript doesn't have native support for 2D arrays unless you write your own code for one. The way I've always done it is to initialise a single dimensional array and then populate it with empty arrays using a for-loop, you have to tell it how many you want by setting the counter in the loop (3 in this example).

You were also accessing the elements incorrectly, I do it the same as Java does and use two seperate arguments [][] and not one with a comma seperating them. I'm not sure if the way you did it is valid, I've not seen it used before.

Code:
	var arrAddresses = new Array();
	for (i = 0; i < 3; i++)
		arrAddresses[i] = new Array();
	
			
	arrAddresses[0][0] = '23 Any Lane'
	arrAddresses[0][1] = 'Any Town'
	arrAddresses[0][2] = 'Anywhere'
	arrAddresses[0][3] = ''
	arrAddresses[0][4] = ''
	arrAddresses[0][5] = 'AN1 3ER'
	arrAddresses[0][6] = 'GB'
	arrAddresses[0][7] = '0207 878787'
	
	arrAddresses[1][0] = '25 Some Street'
	arrAddresses[1][1] = 'Some Town'
	arrAddresses[1][2] = 'Somewhere'
	arrAddresses[1][3] = ''
	arrAddresses[1][4] = ''
	arrAddresses[1][5] = 'SO4 3WH'
	arrAddresses[1][6] = 'GB'
	arrAddresses[1][7] = '0208 272727'
	
	arrAddresses[2][0] = 'Lorem Ipsum'
	arrAddresses[2][1] = 'Dolor sit'
	arrAddresses[2][2] = 'Amet consectetuer'
	arrAddresses[2][3] = 'Adipiscing'
	arrAddresses[2][4] = 'Elit'
	arrAddresses[2][5] = 'AB12 3CD'
	arrAddresses[2][6] = 'GB'
	arrAddresses[2][7] = '01234 567890'
	
	function setAddress(szSelectedAddress) {				
		alert(szSelectedAddress)
		
		alert(arrAddresses[szSelectedAddress][1])
	}
 
Hi,

Couple of things wrong here, firstly Javascript (like most, if not all, of the curly braced languages) supports multi-dimensional arrays only by arrays of arrays.

Second problem is the use of the comma operator. If you wrote:

var i = (2,3);

I would equal 3, always. The comma operator evaluates both terms, but only returns the term on the farthest right! Thus:

arrAddresses[0,0] = '23 Any Lane'
...
arrAddresses[2,0] = '25 Some Street'
...
arrAddresses[3,0] = 'Lorem Ipsum'

Will always assign element 0 (zero) of the array.

By the way, you don't need to explicitly use the constructor for Array here, using [] will do...

Here is an example...

Code:
var arrAddresses = [];
			
	arrAddresses[0] = ['23 Any Lane','Any Town','Anywhere','AN1 3ER','GB','0207 878787']
	
	arrAddresses[1] = ['25 Some Street','Some Town','Somewhere','SO4 3WH','GB','0208 272727']
	
	arrAddresses[2] = ['Lorem Ipsum','Dolor sit','Amet consectetuer','Adipiscing','Elit','AB12 3CD','GB','01234 567890'];
	
	function setAddress(szSelectedAddress) {				
		var full_address = "";
		for(var i = 0; i < szSelectedAddress.length; i++) {
			full_address += szSelectedAddress[i];
			full_address += i < szSelectedAddress.length-1 ? "\r\n" : "";
		}
		alert(full_address );

	}

	for(var i = 0; i < arrAddresses.length; i++) {
		setAddress(arrAddresses[i]);
	}

	alert("Example of how the comma operator works: " + (99,2) + "\r\nIt will only ever return 2 because it evaluates all values but 

only returns the one on the right...");


Best wishes,

Steve
 
Last edited:
Back
Top Bottom