echofool Posted March 6, 2012 Share Posted March 6, 2012 Hey Need help with creating an array in javascript. I did this: var positions = new Array();positions[0][0] = 0;positions[1][0] = 1;positions[0][1] = 1;positions[1][1] = 1; The idea i had was the two sqaure brackets will represet a map grid reference, but i was wondering why it was not working.. I get this error: TypeError: Cannot set property '0' of undefined Any ideas why this happens ? Hope you can help! Link to comment Share on other sites More sharing options...
justsomeguy Posted March 6, 2012 Share Posted March 6, 2012 positions[0] and positions[1] are undefined, you need to make them an array also before adding items to them. You can also use shorthand: var positions = [ [0, 1], [1, 1]]; Link to comment Share on other sites More sharing options...
echofool Posted March 6, 2012 Author Share Posted March 6, 2012 Well the array im suppose to be creating from a database which has: x : y : image so could the array be made like to save lines of code? var positions = [ [0, 1] = 1, [1, 1] = 1 ]; Link to comment Share on other sites More sharing options...
dsonesuk Posted March 6, 2012 Share Posted March 6, 2012 Not like that, like thissss would be better var positions = [ [0, 1, '1'], [1, 1, '1'] ]; Same as used to set up google map coords, title, descriptions var locations = [['In reprehenderit in reprehenderit', -13.530825,-71.957565, 'esse cillum dolore excepteur sint occaecat. Sed do eiusmod tempor incididunt consectetur],['reprehenderit', -13.531211,-71.961921, 'Cupidatat non proident, lorem ipsum dolor sit amet, ullamco laboris nisi..'] ]; Link to comment Share on other sites More sharing options...
ShadowMage Posted March 6, 2012 Share Posted March 6, 2012 Well the array im suppose to be creating from a database which has: x : y : image so could the array be made like to save lines of code? var positions = [ [0, 1] = 1, [1, 1] = 1 ]; What are you trying to accomplish there? Are you trying to set positions[0][1] = 1 and positions[1][1] = 1?If that was your goal, the values are already set with what JSG posted.var positions = [ [0,1], //This line refers to positions[0] [1,1] //This line refers to positions[1]] The comma separated values in each line correspond to the second dimension of the array. So the first 0 is in the first line so the first array dimension is positions[0], it is also the first comma separated value on that line so the second array dimension is positions[0][0]. So positions[0][0] is equal to 0. The 1 on the same line corresponds to positions[0][1], so positions[0][1] is equal to 1. And so on... Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.