declare

Creating two dimensional array in javascript



Sharing buttons:

this is part 23 of JavaScript tutorial

in this video well disc is creating

two-dimensional arrays in JavaScript

JavaScript does not have any special

syntax for creating multi-dimensional

arrays instead we create an array of

arrays the example that we have here

creates a two dimensional three by three

array that is an array which is got

three rows and three columns if you look

at element one that's present in row 0

column 0 element 5 is present in Row 1

column 1 and element 9 is present in Row

2 column 2 let's see how to create this

two-dimensional array with three rows

and three columns now a

multi-dimensional array in a Java Script

is actually an array of arrays now in

this example we want to create an array

with three rows and three columns so

first we need to create an array let's

name it my array 1 equals new array and

the size is going to be 3 because we

want 3 rows and what is a

multi-dimensional array in JavaScript

it's an array of arrays we already have

an array with 3 storage locations now we

want to create an array of arrays so

within each storage location of this

array we are going to create another

array with 3 elements so to loop through

each storage location in my array 1 I am

going to use a for loop so for where I

equals 0 I less than 3 I plus plus

my RA one of I equals new array of three

so what is this code going to do for us

within each storage location of this my

array one we are creating another array

with size three okay so now we have got

an array with three rows and three

columns so now we need to populate each

storage location within this my array

one so my array one of true zero column

zero we want to store number one

similarly in my array of row 0 column 1

we want to store number 2 row 0 column 2

we want to store number 3 in the second

row we want to store 4 5 6 so in column

1 we want for column 2 5 and column I

mean column 0 for column 5 1 and column

2 6 of Row 1 to speed things up I have

already typed the required code so let's

paste it here so in the second row we

have 4 5 6 and in the third row that is

the row with index position 2 we have 7

8 9 so we have populated all the storage

locations within this my array 1 now

let's use a nested for loop loop through

each storage location retrieve the

number and then write it to the web page

so I'm going to use a nested for loop

here so for where I equals 0 I less than

3 I plus plus so let's make a copy of

this one and paste it there and let's

rename this variable to j

so we have a nested for loop here

retrieve my RA one of I J and we want to

write the value that is present at that

storage location to the document so

let's use the write method after

printing each number we want a tab space

so I'm going to use ampersand EMS P

semicolon and after printing each row of

numbers we want an HTML break so outside

of the inner for loop I'm going to use

another document dot write and then spit

out HTML break alright so with all these

changes let's run this and as you might

expect we should have numbers from 1 to

9 printed on the webform now if you look

at the code that we have here we have

hard-coded the index positions here and

then storing the numbers within each

individual storage location of this

array to reduce the amount of code we

have to write we can use a nested for

loop like this and then store the

numbers so let me make a copy of this

nested for loop and let's paste it right

here so we can get rid of this HTML

break and now I'm going to use my array

1 of I of J so we need to populate this

with a value for that what I'm going to

use is another variable here let's call

it start initialize that to 1 and then

we are going to store whatever value we

have in this start variable and every

time we want to increment that by 1 so

now when we run this we should still get

the same output that is numbers 1 2 9

printed in three rows okay so obviously

using this nested for loop has reduced

the amount of code that we have to write

and that is this example

now let's see how to perform an addition

between two two-dimensional arrays in

JavaScript so what we want to do is we

want to have a three by three array like

this which is going to store numbers

from one to nine and we want another

two-dimensional array which is going to

store numbers from nine to one okay and

we want to add these two two-dimensional

arrays and the result should be stored

in another two-dimensional array and we

should get this result so when we add

nine to one the result is ten similarly

when we add 8 to 2 the result is 10 5 to

5 it is 10 so in all these storage

locations within the result we should

get number 10 so let's see how to

achieve this so at the moment we have we

already have one two-dimensional array

which has got numbers from 1 to 9 so

that's my array 1 so what I'm going to

do is actually make a copy of this one

and after you know the first array we

want this plus symbol so what I'm going

to do is document dot write write an

HTML break and then let's actually use

ampersand EMS P semicolon which is going

to give us that tab space and then let's

print that plus symbol and then let's

plant to HTML breaks and now we want our

second array so let's make a copy of

this piece of code here and paste it

right here and let's rename this array

name from my array 1 to my array to my

array 1 let's rename that to my array to

replace all occurrences

and in myarray - we should have numbers

from nine to one so we should be

decrementing it okay so by the time it

has reached this line this start

variable will have a value of ten okay

so now in here we are creating my array

two with three storage locations and

then within each storage location we are

creating another array with three

elements so again this is a two

dimensional three by three array now we

don't have to initialize this variable

once again we already have the start

variable and its value at the moment is

number ten so what I'm going to do is

actually move this right here and then

subtract one from it

so ten minus one start variable at the

moment contains nine and we are storing

nine in zero comma zero that is row 0

and column zero and this loop will

continue to populate all the storage

locations from nine to one and then

finally we are using a nested for loop

and printing out the values to the web

page so let's quickly run this and see

if we get the output that we expect so

at the moment we should have two arrays

first array should have one to nine and

second array should have nine to one now

we want to perform addition operation

between those two arrays so first of all

let's make a copy of these lines and we

want equal to symbol so instead of that

plus let's paste that equal to I'm going

to make a copy of this piece of code so

let's copy that

and paste it right there and let's

rename my RA to two myarray three

replace all the occurrences so this is

what is going to populate the storage

locations within my RA 3 now what do we

want to store in my array 3 we want to

add the elements that are present in my

RA 1 and my RA 2 in the respective

storage locations and whatever is the

sum that we get we want to store that in

the corresponding storage location of my

RA 3 so we don't need this start

variable here so let's get rid of that

and then here we are going to use my RA

1 of I of J plus my RA 2 of I of J and

then finally retrieve the values and

print them to the console I mean to the

document using this nested follow so

let's quickly run this and see if we get

the output look at that we get the

output that we expect so pretty

straightforward example there now so far

we have seen how to create a

two-dimensional 3x3 arrays now let's

look at another example of creating a

two-dimensional 3x5 array that is 3 rows

and 5 columns so I'm going to get rid of

all this code so let's delete this code

so at the moment we have the code for my

array1 and if you look at this my array

one it has got three storage locations

now we want to create a two-dimensional

array with three rows and five columns

meaning you know the outer array is

going to contain three storage locations

and within each storage location we want

to have five elements right so this for

loop right here is going to run until

five and we are going to create another

array with five elements so this

actually going to run till three because

in the outer story our array we only

have three storage locations so three

rows and within each row we want five

columns so from 0 till three and then

within each storage location of this

array create another array with five

elements and we are going to start at

100 so we are using a for loop here to

populate each storage location within

the array so the outer for loop is going

to run from 0 to 3 the n now for loop is

going to run from 0 to 5 and we are

going to increment you know the start

variable by 1 and within each storage

location we are storing whatever value

we have caught in that variable and

finally we use another nested for loop

here to print out the values so the

inner for loop should run from 0 to 5 so

when we run this as you might expect we

should have an array with 3 rows and 5

columns so it's that simple to create a

two-dimensional array in JavaScript

thank you for listening and have a great

day