How to Combine Two Array Into One Object in JavaScript

How to Combine Two Array Into One Object in JavaScript

Introduction

In this tutorial we will create a How to Combine Two Array Into One Object in JavaScript. This tutorial purpose is to teach you how to combine your two array into one. This will tackle all the basic function that will combine the array you have. 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 uses array as your data. I will give my best to provide you the easiest way of creating this program Combine array. 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 array list form and button. 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">Sourcecodester</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 Combine Two Array Into One Object</h3>
  15.                 <hr style="botder-top:1px dotted #ccc;" />
  16.                 <div class="col-md-6">
  17.                         <h3>Array 1</h3>
  18.                         <pre id="result1"></pre>
  19.                        
  20.                         <br />
  21.                        
  22.                         <h3>Array 2</h3>
  23.                         <pre id="result2"></pre>
  24.                 </div>
  25.                 <div class="col-md-6">
  26.                         <center><button class="btn btn-primary" id="merge">Merge</button></center>
  27.                         <br />
  28.                         <table class="table table-bordered">
  29.                                 <thead>
  30.                                         <tr>
  31.                                                 <th>New Array</th>
  32.                                         </tr>
  33.                                 </thead>
  34.                                 <tbody id="display"></tbody>
  35.                         </table>
  36.                 </div>
  37.  
  38.         </div>
  39. <script src="script.js"></script>      
  40. </body>
  41. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will combine your two array when the button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. var arr1 = ["Elephant", "Cat", "Lion", "Bear"];
  2. var arr2 = ["Dog", "Giraffe", "Tiger", "Eagle"];
  3.  
  4.  
  5. function displayArray(array1, array2){
  6.         document.getElementById('result1').innerHTML=array1;
  7.         document.getElementById('result2').innerHTML=array2;
  8. }
  9.  
  10.  
  11. function combineArray(array1, array2){
  12.         var merge = array1.concat(array2);
  13.         var html ="";
  14.         for(var i=0; i<merge.length; i++){
  15.                 html+="<tr>";
  16.                 html+="<td>"+merge[i]+"</td>";
  17.                 html+="</tr>";
  18.         }
  19.        
  20.         document.getElementById('display').innerHTML=html;
  21. }
  22.  
  23. displayArray(arr1, arr2);      
  24.  
  25. document.getElementById('merge').addEventListener('click', combineArray.bind(this, arr1, arr2));

In the code above we first create our two array and set it into a variables. We will now then create a method that will combine array into one, we will call it combineArray(). This function will combine your two array using the concat() function, this will take two array to merge into one array.

Output:

The How to Combine Two Array Into One Object 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 Combine Two Array Into One Object 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