Welcome
Login / Register

Creating two dimensional array in javascript

Featured video
You need to have the Flash Player installed and a browser with JavaScript support.

Thanks! Share it with your friends!

URL

You disliked this video. Thanks for the feedback!

URL


Published by Admin in Java Script
864 Views

Description

Link for all dot net and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists

Link for slides, code samples and text version of the video
http://csharp-video-tutorials.blogspot.com/2014/12/creating-two-dimensional-array-in.html

JavaScript does not have a special syntax for creating multidimensional arrays. Instead we create an array of arrays.

Example : The following JavaScript code creates a 2 dimensional 3x3 array

var myArray1 = new Array(3)

for (i=0; i [ 3; i++)
myArray1[i]=new Array(3)

myArray1[0][0] = "1"
myArray1[0][1] = "2"
myArray1[0][2] = "3"

myArray1[1][0] = "4"
myArray1[1][1] = "5"
myArray1[1][2] = "6"

myArray1[2][0] = "7"
myArray1[2][1] = "8"
myArray1[2][2] = "9"

for (var i = 0; i [ 3; i++)
{
for (var j = 0; j [ 3; j++)
{
document.write(myArray1[i][j] + " ");
}
document.write("[br/]");
}

In Example 1, we have manually populated each storage location in the array. Instead we can use to 2 nested for loops as shown below.

var myArray1 = new Array(3)

for (i=0; i [ 3; i++)
myArray1[i]=new Array(3)

var start = 1;

for (var i = 0; i [ 3; i++)
{
for (var j = 0; j [ 3; j++)
{
myArray1[i][j] = start;
start = start + 1;
}
}


for (var i = 0; i [ 3; i++)
{
for (var j = 0; j [ 3; j++)
{
document.write(myArray1[i][j] + " ");
}
document.write("[br/]");
}

Performing addition between 2 two dimensional arrays in JavaScript

Example :
The first 3 x 3 array should contain numbers from 1 to 9
The second 3 x 3 array should contain numbers from 9 to 1
The numbers present at each index position in the first and second array should be added and the result should be stored in a third 3x3 array.

// Create the first 2 dimensional 3 X 3 array
var myArray1 = new Array(3)

for (i=0; i [ 3; i++)
myArray1[i]=new Array(3)

var start = 1;

for (var i = 0; i [ 3; i++)
{
for (var j = 0; j [ 3; j++)
{
myArray1[i][j] = start;
start = start + 1;
}
}

for (var i = 0; i [ 3; i++)
{
for (var j = 0; j [ 3; j++)
{
document.write(myArray1[i][j] + " ");
}
document.write("[br/]");
}

document.write("[br/]");
document.write(" +");
document.write("[br/]");
document.write("[br/]");

// Create the second 2 dimensional 3 X 3 array
var myArray2 = new Array(3)

for (i = 0; i [ 3; i++)
myArray2[i] = new Array(3)

for (var i = 0; i [ 3; i++)
{
for (var j = 0; j [ 3; j++)
{
start = start - 1;
myArray2[i][j] = start;
}
}

for (var i = 0; i [ 3; i++)
{
for (var j = 0; j [ 3; j++)
{
document.write(myArray2[i][j] + " ");
}
document.write("[br/]");
}

document.write("[br/]");
document.write(" =");
document.write("[br/]");
document.write("[br/]");

// Create the third 2 dimensional 3 X 3 array
var myArray3 = new Array(3)

for (i = 0; i [ 3; i++)
myArray3[i] = new Array(3)

for (var i = 0; i [ 3; i++)
{
for (var j = 0; j [ 3; j++)
{
myArray3[i][j] = myArray1[i][j] + myArray2[i][j];
}
}


for (var i = 0; i [ 3; i++)
{
for (var j = 0; j [ 3; j++)
{
document.write(myArray3[i][j] + " ");
}
document.write("[br/]");
}

Creating a 2 dimensional 3 X 5 array

var myArray1 = new Array(3)

for (i=0; i [ 3; i++)
myArray1[i]=new Array(5)

var start = 101;

for (var i = 0; i [ 3; i++)
{
for (var j = 0; j [ 5; j++)
{
myArray1[i][j] = start;
start = start + 1;
}
}

for (var i = 0; i [ 3; i++)
{
for (var j = 0; j [ 5; j++)
{
document.write(myArray1[i][j] + " ");
}
document.write("[br/]");
}

Show more

Post your comment

Comments

Be the first to comment