How to Copy an Array to Other Array in JavaScript

How to Copy an Array to Other Array in JavaScript

Introduction

In this tutorial we will create a How to Copy an Array to Other Array in JavaScript. This tutorial purpose is to teach you how to copy an array to another array. This will tackle all the basic function that will copy the array. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application that use array as a temporary data storage. I will give my best to provide you the easiest way of creating this program Calculate GPA automatically. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the tables and buttons. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodster</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">How to Copy an Array to Other Array</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                         <div class="col-md-4">
  17.                                 <form action="javascript:void(0);" method="POST" class="form-group" onsubmit="insert()">
  18.                                         <label>Enter here</label>
  19.                                         <input type="text" id="name" class="form-control"/>
  20.                                         <br />
  21.                                         <button type="submit" class="btn btn-primary form-control">Save</button>
  22.                                 </form>
  23.                         </div>
  24.                         <div class="col-md-3">
  25.                                 <table class="table table-bordered">
  26.                                         <thead class="alert-info">     
  27.                                                 <tr>
  28.                                                         <th>Array 1</th>
  29.                                                 </tr>
  30.                                         </thead>
  31.                                         <tbody id="list1">
  32.                                         </tbody>
  33.                                 </table>
  34.                         </div> 
  35.                         <div class="col-md-2">
  36.                                 <button class="btn btn-default" id="copy" onclick="copyArray(this);" disabled="disabled">Copy</button>
  37.                                 <br /><br />
  38.                                 <button class="btn btn-default" id="clear" onclick="clearAll(this);" disabled="disabled">Reset</button>
  39.                         </div>
  40.                         <div class="col-md-3">
  41.                                 <table class="table table-bordered">
  42.                                         <thead class="alert-info">     
  43.                                                 <tr>
  44.                                                         <th>Array 2</th>
  45.                                                 </tr>
  46.                                         </thead>
  47.                                         <tbody id="list2">
  48.                                         </tbody>
  49.                                 </table>
  50.                         </div> 
  51.                        
  52.         </div>
  53. </body>
  54. <script src="script.js"></script>
  55. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will copy the array to other array when the button copy is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. let list1 = document.getElementById('list1');
  2. let array1 = [];
  3. let array2 = [];
  4.  
  5.  
  6. function arrayList1() {
  7.         let data = '';
  8.         if (array1.length > 0) {
  9.                 for (var i = 0; i < array1.length; i++) {
  10.                         data += '<tr>';
  11.                         data += '<td>' + array1[i] + '</td>';
  12.                         data += '</tr>';
  13.                 }
  14.         }
  15.         list1.innerHTML = data;
  16. };
  17.  
  18. function arrayList2() {
  19.         let data = '';
  20.         if (array2.length > 0) {
  21.                 for (var i = 0; i < array2.length; i++) {
  22.                         data += '<tr>';
  23.                         data += '<td>' + array2[i] + '</td>';
  24.                         data += '</tr>';
  25.                 }
  26.         }
  27.        
  28.         list2.innerHTML = data;
  29. };
  30.  
  31.  
  32. function copyArray(btn){
  33.         if(array1.length > 0){
  34.                 for (var i = 0; i < array1.length; i++) {
  35.                         array2.push(array1[i]);
  36.                 }
  37.                
  38.                 arrayList2();
  39.         }
  40.        
  41.         btn.setAttribute("disabled", "disabled");
  42.         document.getElementById('clear').removeAttribute('disabled');
  43. }
  44.  
  45. function clearAll(btn){
  46.         array2=[];
  47.         document.getElementById('list2').innerHTML = "";
  48.         document.getElementById('copy').removeAttribute('disabled');
  49.         btn.setAttribute("disabled", "disabled");
  50. }
  51.  
  52. function insert() {
  53.     let el = document.getElementById('name');
  54.         let name = el.value;
  55.         if (name) {
  56.                 array1.push(name.trim());
  57.                 el.value = '';
  58.         }
  59.         arrayList1();
  60.        
  61.         if (array2.length == 0) {
  62.                 document.getElementById('copy').removeAttribute('disabled');
  63.         }
  64. };
  65.        
  66.        

In the code above we created multiple methods that will handle the array data. In order to copy an array we will first create a method called copyArray(). This function will copy the existing array to an empty array object by pushing using the push() function.

Output:

The How to Copy an Array to Other Array in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Copy an Array to Other Array in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment