/    Sign up×
Community /Pin to ProfileBookmark

2D array in JS.

How do you create and access a 2D Array in Javascript?

Thanks,
Jason.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@so_is_thisNov 25.2006 — [CODE]
var myArray = new Array(
new Array(0, 1, 2),
new Array(1, 2, 3)
);
alert(myArray[0][2]); // displays 2
alert(myArray[1][2]); // displays 3[/CODE]
Copy linkTweet thisAlerts:
@RetroFiestaauthorNov 25.2006 — Thank you.

Jason.
Copy linkTweet thisAlerts:
@KorNov 25.2006 — Or you may use more friendly notation in [URL=http://www.json.org]JSON[/URL] :
<i>
</i>var myArray=[
[0,1,2],
[1,2,3]
]
Copy linkTweet thisAlerts:
@so_is_thisNov 25.2006 — Thank you.

Jason.[/QUOTE]

You're welcome.

Salud!
Copy linkTweet thisAlerts:
@PeterKieuDec 04.2007 — Hi man!

I use my function to define an 2d array. Take a look:

function KArray2D(NumOfRows,NumOfCols)

{

var k=new Array(NumOfRows);

for (i = 0; i < k . length; ++ i)

k [i] = new Array (NumOfCols);



return k;
}

Usage:

hiYou = new KArray2D(3,3); //3x3 array, or arbitrary

hiYou[0][0] = "Peter";

hiYou[0][1] = "Kieu";

hiYou[0][2] = "says";

hiYou[1][0] = "How";

hiYou[1][1] = "are";

hiYou[1][2] = "you";

hiYou[2][0] = "?";

hiYou[2][1] = "O";

hiYou[2][2] = "K";

Test: create file "test2D-array.htm" which has following content:

<html>

<body>

<script type="text/javascript">

function KArray2D(row,col)

{

var k=new Array(row);

for (i = 0; i < k . length; ++ i)

k [i] = new Array (col);



return k;
}
hiYou = new KArray2D(3,3); //3x3 array, or arbitrary
hiYou[0][0] = "Peter ";
hiYou[0][1] = "Kieu ";
hiYou[0][2] = "says:";
hiYou[1][0] = "How ";
hiYou[1][1] = "are ";
hiYou[1][2] = "you ";
hiYou[2][0] = "?";
hiYou[2][1] = "O";
hiYou[2][2] = "K";
document.write("Using 2d-array in JavaScript by Peter Kieu<br/>");
document.write(hiYou[1][0]+hiYou[1][1]+hiYou[1][2]+hiYou[2][0]);

</script>

</body>

</html>

Run in IE or FireFox, I get:

Using 2d-array in JavaScript by Peter Kieu

How are you ?
Copy linkTweet thisAlerts:
@RetroFiestaauthorDec 05.2007 — That is brilliant. Thank you.

It's always nice to see a practical ( working ) example of some code.

Jason.
Copy linkTweet thisAlerts:
@PeterKieuDec 05.2007 — If you need more space in array, here is 3D array. 4/5/6D any-D array comes from same idea depends on JavaScript engine in IE/FireFox/Opera/Safari...

function KArray3D(row,col,high)

{

var k=new Array(row);

for (i = 0; i < k . length; ++ i)

{

k [i] = new Array (col);

for (j = 0; j < k[i] . length; ++ j)

k [i][j] = new Array (high);

}

return k;

}



Tested same as above 2D.

Have a good job!
×

Success!

Help @RetroFiesta 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.20,
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,
)...