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>
<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
Users do not appreciate simultaneous cross-posting of Mickey Mouse questions like this into every forum you can find. Had you put the search engine to proper use, you could have found plenty of answers without asking.
https://developer.mozilla.org/en-US/...nsional_Arrays
Where used, return should be executed unconditionally and always as the last statement in the function.
That's my signature, it's not part of the damn post!
I did this ....
Code:
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];
var i = 0;
for (i; i < myArray.length; i++) {
var result = myArray;
$('div').html(result);
}
HTML Code:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" > </script>
<meta charset=utf-8 />
<title> JS Bin</title>
</head>
<body>
<div> </div>
</body>
</html>
Here: http://jsbin.com/uyefuf/1/edit/
Originally Posted by
shinamee
I did this ....
Code:
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];
var i = 0;
for (i; i < myArray.length; i++) {
var result = myArray;
$('div').html(result);
}
HTML Code:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" > </script>
<meta charset=utf-8 />
<title> JS Bin</title>
</head>
<body>
<div> </div>
</body>
</html>
Here:
http://jsbin.com/uyefuf/1/edit/
Why bother with the entire 'jquery' library for this: $('div').html(result);
when all you needed to change was this
Code:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="divBox"></div>
<script type="text/javascript">
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.concat(m1,m2,m3);
document.getElementById('divBox').innerHTML = myArray;
</script>
</body>
</html>
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks