How to Delete JSON Element from an Array in JavaScript

How to Delete JSON Element from an Array in JavaScript

Introduction

In this tutorial we will create a How to Delete JSON Element from an Array in JavaScript. This tutorial purpose is to teach you how to delete an array from JSON object. This will thoroughly show you the simple way deleting an 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 array for display data elements. I will give my best to provide you the easiest way of creating this program Delete an array from JSON object. So let's do the coding.

Before we get started:

Here 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 html form and table. 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 Delete JSON Element from an Array</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-8">
  17.                         <table class="table table-bordered">
  18.                                 <thead class="alert-info">
  19.                                         <tr>
  20.                                                 <th>Firstname</th>
  21.                                                 <th>Lastname</th>
  22.                                                 <th>Address</th>
  23.                                                 <th>Action</th>
  24.                                         </tr>
  25.                                 </thead>
  26.                                 <tbody id= "result"></tbody>
  27.                         </table>
  28.                 </div>
  29.                 <div class="col-md-4">
  30.                         <div class="form-group">
  31.                                 <label>Firstname</label>
  32.                                 <input type="text" id="firstname" class="form-control"/>
  33.                         </div>
  34.                         <div class="form-group">
  35.                                 <label>Lastname</label>
  36.                                 <input type="text" id="lastname" class="form-control"/>
  37.                         </div>
  38.                         <div class="form-group">
  39.                                 <label>Address</label>
  40.                                 <input type="text" id="address" class="form-control"/>
  41.                         </div>
  42.                         <div class="form-group">
  43.                                 <button type="button" onclick="saveData()" class="btn btn-primary form-control">Save Data</button>
  44.                         </div>
  45.                 </div>
  46.         </div>
  47. </body>
  48. <script src="script.js"></script>
  49. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will allow you to delete an array from JSON object. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. var data = [];
  2.        
  3. function displayData(){
  4.        
  5.         var table = "" ;
  6.                
  7.                         for(var i in data){
  8.                                 table += "<tr>";
  9.                                 table += "<td>"
  10.                                                 + data[i].firstname +"</td>"
  11.                                                 + "<td>" + data[i].lastname +"</td>"
  12.                                                 + "<td>" + data[i].address +"</td>"
  13.                                                 + "<td><button class='btn btn-danger btn-sm' onclick='deleteData("+i+")'>Delete</button></td>";
  14.                                 table += "</tr>";
  15.                         }
  16.                        
  17.         document.getElementById("result").innerHTML = table;           
  18. }
  19.  
  20. function saveData(){
  21.         let firstname = document.getElementById("firstname");
  22.         let lastname = document.getElementById("lastname");
  23.         let address = document.getElementById("address");
  24.        
  25.         if(firstname.value == "" || lastname.value == "" || address.value == ""){
  26.                 alert("Please complete the required field first!");
  27.         }else{
  28.                 var member = {
  29.                         'firstname': firstname.value,
  30.                         'lastname': lastname.value,
  31.                         'address': address.value,
  32.                 };
  33.                
  34.                 data.push(member);
  35.                
  36.                 displayData();
  37.                
  38.                
  39.                 firstname.value = "";
  40.                 lastname.value = "";
  41.                 address.value = "";
  42.                
  43.         }
  44. }
  45.  
  46. function deleteData(index){
  47.         delete data[index];
  48.         alert("You have deleted a row");
  49.         displayData();
  50. }

In the code above we just create one method to delete an array from the JSON object called deleteData(). This function will dynamically delete the array data in the table when you clicked the delete button.

Output:

The How to Delete JSON Element from an 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 Delete JSON Element from an 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