Re: [jQuery] Javascript Array question
2009/11/13 Atkinson, Sarah <Sarah.Atkinson@cookmedical.com>:
> I want to create an 2D array that would basically contain a variable amount
> of 3 entry arrays. Is there a short way of doing this? Like
> var myArray= new Array( ,3);
JavaScript doesn't support multi-dimensional arrays, so you'll have to
work with a single-dimensional array of single-dimensional arrays.
Furthermore, JavaScript allows for arrays to be dynamically resized,
so you can specify the convention that your arrays should be of length
3, but the language will not enforce that constraint.
For example:
var example = [
[0, 1, 2],
[4, 5, 6],
[7, 8, 9]
];
// that gives us an array containing three arrays, each of length 3
alert(example[0][0]); // alerts "0"
alert(example[1][2]); // alerts "6"
example[0][749] = "Foo";
alert(example[0].length); // alerts "750"
alert(example[0][2]); // alerts "2"
alert(example[0][3]); // alerts "undefined"
alert(example[0][748]); // alerts "undefined"
alert(example[0][749]); // alerts "Foo"
Regards,
Nick.
--
Nick Fitzsimons
http://www.nickfitz.co.uk/
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home