In Association with Amazon.in   Flipkart

Friday, September 27, 2013

JavaScript Multi-Dimensional Array With Example

Multi Dimensional Array:


Two/Multi-Dimensional Array is nothing but an Array in another Array

In an array each element is assigned with another array and so on....
i.e 2D Array, 3D Array, .....


Example:

<script>                  
var a = new Array(1234, "Kumar", "MGR", 35000);    // 1D Array               
var b = new Array(1534, "Ravi", "Acc", 25000);     // 1D Array  
var c = new Array(1643, "Hari", "HR", 15000);       // 1D Array                 
var emp = new Array(a, b, c);  //  2D Arry

document.write("<br/>emp Array length: " + emp.length);

//  To print All emp array elements
for(i=0; i<emp.length; i++)
{
    document.write("<br/>");
        for(j=0; j<emp[i].length; j++)
        document.write(emp[i][j] + "   ");
}

//  To print All emp array elements in the Table
document.write('<table align="center" border="5" width="400" ><tr><th>E.No</th><th>EName</th><th>Job</th><th>Sal</th></tr>');

for(i=0; i<emp.length; i++)
{
    document.write("<tr>");
        for(j=0; j<emp[i].length; j++) {
        document.write("<td>"+ emp[i][j] + "</td>");
        }
    document.write("</tr>");
}

document.write("</table>");
</script>


Output:


emp Array length: 3
1234 Kumar MGR 35000 
1534 Ravi Acc 25000 
1643 Hari HR 15000
E.NoENameJobSal
1234KumarMGR35000
1534RaviAcc25000
1643HariHR15000

No comments:

Post a Comment


Related Posts Plugin for WordPress, Blogger...