Blogroll

Powered by Blogger.

Thursday, 12 June 2014

Program to Copy a Multi Dimensional JavaScript Array to Another Similar Array

by realfinetime  |  in JavaScript 2D Array at  22:30

In certain situations, we have to copy contents from an array to an empty array. Empty array, as it's name indicates, will have no size limit and should be extendable. Declaration of empty arrays and copying contents from one array to empty array is simple. But it has some confusing syntaxes. Following program is a simple demonstration of these confusing tasks. To run this code, copy the program given below to an HTML file. Then open this HTML file in a web browser.



<script type="text/Javascript">

var empty_multi_dim_array=[[]];             // Declare empty array
var multi_dim_array_with_contents = [

['mango'   ,'orange' ,'banana'  ],
['ford'    ,'audi'   ,'benz'    ],
['control' ,'alt'    ,'delete'  ]

];                                                              // Declare array having contents

/** PRINT CONTENTS OF multi_dim_array_with_contents ARRAY **/

for(var i=0;i<multi_dim_array_with_contents.length;i++)
{
for(var j=0;j<multi_dim_array_with_contents[i].length;j++)
{
document.write(multi_dim_array_with_contents[i][j]);
document.write("  ,  ");
}
document.write("<br></br>");
}

/** COPY CONTENTS OF multi_dim_array_with_contents ARRAY TO empty_multi_dim_array ARRAY **/

for(i=0;i<multi_dim_array_with_contents.length;i++)
{
empty_multi_dim_array[i] = multi_dim_array_with_contents[i];
}

/** PRINT CONTENTS OF empty_multi_dim_array ARRAY **/

document.write("<br></br>");

for(i=0;i<empty_multi_dim_array.length;i++)
{
for(j=0;j<empty_multi_dim_array[i].length;j++)
{
document.write(empty_multi_dim_array[i][j]);
document.write("  ,  ");
}
document.write("<br></br>");
}

</script>

Output of the above program is given below. First one is the original array and the second one is the copied array.

0 comments:

IMPORTANT NOTICE

All the circuits, published in this blog is only after testing and getting proper results in my private lab. When you try these circuits, you should check the supply voltage, polarity of components, presence of childrens nearby and shorts in the circuits. This website will not be responsible for any harm happened to you or your components caused by your carelessness.

For More Electronic Tips

Proudly Powered by Blogger.