Jump to content

Javascript array


echofool
 Share

Recommended Posts

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! Edited by echofool
Link to comment
Share on other sites

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..'] ];

Edited by dsonesuk
Link to comment
Share on other sites

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...