How to Merge Two JSON into One Array Object in JavaScript

Introduction

In this tutorial we will create a How to Merge Two JSON into One Array Object. This tutorial major purpose is to provide an efficient use of array objects. This will tackle the merging of JSON object into one array object. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is very easy to create just follow the instruction and you will do it without a problem. This program is useful if you are working with arrays and want to merge multiple arrays into one. I will give my best to give you the easiest way of creating this program How to Merge Two JSON into One Array Object. 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 display an interface that will print the array object. 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"/>
  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://www.sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="container-fluid">  
  13.                 <div class="row">
  14.                         <div class="col-md-3"></div>
  15.                         <div class="col-md-6 well">
  16.                                 <h3 class="text-primary">How to Merge Two JSON into One Array Object</h3>
  17.                                 <hr style="border-top: 1px SOLID #8c8b8b;"/>
  18.                                 <div class="row">
  19.                                         <div class="col-md-6">
  20.                                                 <pre id="array1"></pre>
  21.                                         </div>
  22.                                         <div class="col-md-6">
  23.                                                 <pre id="array2"></pre>
  24.                                         </div>
  25.                                 </div>
  26.                                 <br style="clear:both;"/>
  27.                                 <button onclick="mergeArray();">Merge Array</button>
  28.                                 <div class="row">
  29.                                         <div class="col-md-12">
  30.                                                 <pre id="merge"></pre>
  31.                                         </div>
  32.                                 </div>
  33.                         </div>
  34.                 </div>
  35.         </div> 
  36. </body>
  37. <script src="script.js"></script>
  38. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will merge the two JSON array into one JSON array. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. const json1 = [{id:1, firstname: 'John', lastname: 'Smith'}, {id:2, firstname: 'Claire', lastname: 'Temple'}];
  2. const json2 = [{id:3, firstname: 'Jake', lastname: 'Logan'}, {id:4, firstname: 'Kimberly', lastname: 'Morgan'}];
  3.  
  4.  
  5.  
  6. document.getElementById('array1').innerHTML=JSON.stringify(json1, null, 2);
  7. document.getElementById('array2').innerHTML=JSON.stringify(json2, null, 2);
  8.  
  9. function mergeArray(){
  10.         let merge=[json1, json2];
  11.        
  12.         document.getElementById('merge').innerHTML=JSON.stringify(merge , null, 2);
  13. }

In this code we first pre-coded the two JSON array and display it into the HTML page using JSON.stringify(). After that we create a method that will merge the arrays by closing the two JSON array with bracket into a new variable. This will force the two arrays to be compress and append into one array object.

Output:

The How to Merge Two JSON into One Array 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 Merge Two JSON into One Array 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