Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
January 17th, 2013, 08:04 PM
 hiyatran
Contributing User

Join Date: Jun 2010
Posts: 46
Time spent in forums: 4 h 3 m 26 sec
Reputation Power: 4
Other - Multi-Dimensional Array

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, :
Code:
```<!DOCTYPE html>
<html>
<title></title>

<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
__________________
OutingsForLess.com

#2
January 17th, 2013, 09:58 PM
 ZWEI01
Registered User

Join Date: Jan 2013
Posts: 10
Time spent in forums: 5 h 54 m 13 sec
Reputation Power: 0
The problem is you are comparing the length of string "m" + i to j which is two. You have the right idea though.

Code:
`for (var i = 0; i < myArray.length; i++) { for (var j = 0; j < myArray[i].length; j++) { console.log(myArray[i][j]}}`

 Viewing: Dev Shed Forums > Web Design > JavaScript Development > Other - Multi-Dimensional Array