Exploring 2D Arrays in JavaScript

  • What is a 2D Array?
  • In JavaScript, a 2D array is an array of arrays, where each element in the outer array represents a row and each element within those arrays represents a column. It's like a grid where you can store data in rows and columns.

  • Creating 2D Arrays in Python:
  • <!DOCTYPE html> <html> <head> <title></title> </head> <body> <script type="text/javascript"> var a=new Array(2); for(i=0;i<a.length;i++) { a[i]=new Array(2); } </script> </body> </html>

    This creates a 2x2 2D array filled with zeros.


  • Basic Example :
  • <!DOCTYPE html> <html> <head> <title>dd array</title> </head> <body><script type="text/javascript"> var a=new Array(2); for(i=0;i<a.length;i++) { a[i]=new Array(2); } for(i=0;i<2;i++) { for(j=0;j<2;j++) { a[i][j]=parseInt(prompt("enter the no")); } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { document.write(a[i][j]+" "); } document.write("<br>"); } </script> </body> </html>

    Output :
    1 2
    3 4

  • Common Operations on 2D Arrays:
  • Accessing Elements : You can access individual elements in a 2D array using their row and column indices.
    Iterating Through the Array : You can use nested loops to iterate through all elements of the 2D array.
    Searching and Manipulating Data : You can search for specific values or perform manipulations on the data within the 2D array using loops and conditional statements.
    Adding Rows or Columns : You can dynamically add rows or columns to a 2D array.

  • Examples :
  • JavaScript code
    Write a program to perform Addition & Subtraction of Two Matrices using double dimensional array. [4x4 Matrix by default] <!DOCTYPE html> <html> <head> <title>addition and substract</title> </head> <body><script type="text/javascript"> r=parseInt(prompt("enter the row")); c=parseInt(prompt("enter the coloum")); var a=new Array(r); var b=new Array(r); var s=new Array(r); var sub=new Array(r); for(i=0;i<a.length;i++) { a[i]=new Array(c); } for(i=0;i<b.length;i++) { b[i]=new Array(c); } for(i=0;i<s.length;i++) { s[i]=new Array(c); } for(i=0;i<sub.length;i++) { sub[i]=new Array(c); } for(i=0;i<r;i++) { for(j=0;j<c;j++) { a[i][j]=parseInt(prompt("enter the no")); } } document.write("1st matrix is "+"<br>"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { document.write(a[i][j]+" "); } document.write("<br>"); } for(i=0;i<r;i++) { for(j=0;j<c;j++) { b[i][j]=parseInt(prompt("enter the no")); } } document.write("2nd matrix is "+"<br>"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { document.write(b[i][j]+" "); } document.write("<br>"); } for(i=0;i<r;i++) { for(j=0;j<c;j++) { s[i][j]=a[i][j]+b[i][j]; } } for(i=0;i<r;i++) { for(j=0;j<c;j++) { sub[i][j]=a[i][j]-b[i][j]; } } document.write(" matrix addition is "+"<br>"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { document.write(s[i][j]+" "); } document.write("<br>"); } document.write("matrix sub is "+"<br>"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { document.write(sub[i][j]+" "); } document.write("<br>"); } </script> </body> </html>

    Output :
    1st matrix is
    4 5
    6 7
    2nd matrix is
    1 2
    3 4
    matrix addition is
    5 7
    9 11
    matrix sub is
    3 3
    3 3

    JavaScript code
    # Write a program to interchange row and column elements of matrix i.e. transpose. <!DOCTYPE html> <html> <head> <title>transpose</title> </head> <body><script type="text/javascript"> r=parseInt(prompt("enter the row")); c=parseInt(prompt("enter the coloum")); var a=new Array(r); for(i=0;i<a.length;i++) { a[i]=new Array(c); } for(i=0;i<r;i++) { for(j=0;j<c;j++) { a[i][j]=parseInt(prompt("enter the no")); } } document.write("1st matrix is "+"<br>"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { document.write(a[i][j]+" "); } document.write("<br>"); } document.write("transpose is "+"<br>"); for(i=0;i<c;i++) { for(j=0;j<r;j++) { document.write(a[j][i]+" "); } document.write("<br>"); } </script> </body> </html>

    Output :
    1st matrix is
    1 2
    3 4
    transpose is
    1 3
    2 4

  • Conclusion :
  • 2D arrays in JavaScript are versatile data structures that allow you to represent and manipulate tabular data efficiently. Understanding how to work with 2D arrays is essential for many programming tasks, especially when dealing with data organized in rows and columns. By mastering 2D arrays in JavaScript, you gain a powerful tool for handling complex data structures and solving a wide range of programming problems.