Jump to content

multi arrays


locbtran

Recommended Posts

I have an array, called, "myArray". Inside this myArray are 4 more arrays, "m0, m1, m2, m3".How do I access all the elements in all the array? Here's my code, :

<!DOCTYPE html><html><head>	<title></title></head> <body><script>var m0 = [0, 1, 2, 3, 4];var m1 = [5, 6, 7, 8, 9];var m2 = [10, 11, 12, 13, 14];var m3 = [15, 16, 17, 18, 19]; var myArray = [m0, m1, m2, m3];  for (var i = 0; i < myArray.length; i++) {	for (var j = 0; j < ("m"+i).length; j++) {		document.writeln(myArray[i][j]);	}}</script></body></html>

This code only give me the first 2 elements in each array.But, I would like to get all the elements in all the arrays. tks

Link to comment
Share on other sites

You were pretty close.. just add myArray.length; to the second for loop... instead of ("m"+i).length;

<!DOCTYPE html><html><head>        <title></title></head> <body><script>var m0 = [0, 1, 2, 3, 4];var m1 = [5, 6, 7, 8, 9];var m2 = [10, 11, 12, 13, 14];var m3 = [15, 16, 17, 18, 19]; var myArray = [m0, m1, m2, m3];  for (var i = 0; i < myArray.length; i++) {        for (var j = 0; j < myArray[i].length; j++)         {               document.writeln(myArray[i][j]);        }} </script></body></html>

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
×
×
  • Create New...