/    Sign up×
Community /Pin to ProfileBookmark

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>[/CODE]

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

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Logic_AliJan 18.2013 — 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.

[URL]https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Predefined_Core_Objects#Multi-Dimensional_Arrays[/URL]
Copy linkTweet thisAlerts:
@PadonakJan 18.2013 — [b]Just don't say "menu"[/b] ?

...and [b]email/phone/whatever else validation[/b] )) ?
Copy linkTweet thisAlerts:
@shinameeJan 18.2013 — 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);
}
[/CODE]


[code=html]<!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>
[/code]


Here: http://jsbin.com/uyefuf/1/edit/
Copy linkTweet thisAlerts:
@JMRKERJan 18.2013 — 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);
}
[/CODE]


[code=html]<!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>
[/code]


Here: http://jsbin.com/uyefuf/1/edit/[/QUOTE]


?

Why bother with the entire 'jquery' library for this: $('div').html(result);

when all you needed to change was this
<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;title&gt;JS Bin&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id="divBox"&gt;&lt;/div&gt;

&lt;script type="text/javascript"&gt;
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;
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @locbtran spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...