How to Delete Single Array of Data in JavaScript

How to Delete Single Array of Data in JavaScript


In this tutorial we will create a How to Delete Single Array of Data in JavaScript. This tutorial purpose is to teach you to delete a single array. This will cover all the important functionality that allow you to will delete 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 use array as temporary data storage. I will give my best to provide you the easiest way of creating this program Delete Single Array. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface for our application. This code will display the html table and array list. 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="">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 Single Array of Data</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-6">
  17. <pre id="array"></pre>
  18. </div>
  19. <div class="col-md-6">
  20. <table class="table table-bordered">
  21. <thead class="alert-info">
  22. <tr>
  23. <th>Data List</th>
  24. <th>Action</th>
  25. </tr>
  26. </thead>
  27. <tbody id="result"></tbody>
  28. </table>
  29. </div>
  30. </div>
  31. </body>
  32. <script src="script.js"></script>
  33. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will delete the array data when you click the button. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. var members = ["John Smith", "Claire Temple", "John Estrada", "Lito Lapid", "Jack Roberto", "Pauline Luna"];
  3. displayTable();
  4. displayArray();
  6. function deleteArray(index){
  7. alert("You have remove " + members[index]);
  8. members.splice(index, 1);
  9. displayTable();
  10. displayArray();
  11. }
  13. function displayTable(){
  14. var table = "" ;
  16. for(var i=0; i < members.length; i++){
  17. table += "<tr>";
  18. table += "<td>" + members[i] +"</td>"
  19. + "<td><button class='btn btn-danger' onclick='deleteArray("+i+")'>Remove</button></td>";
  20. table += "</tr>";
  22. }
  24. document.getElementById("result").innerHTML = table;
  25. }
  27. function displayArray(){
  28. document.getElementById('array').innerHTML=JSON.stringify(members, null, 2);
  29. }

In the code above we create multiple functions that will handle the displaying of arrays to html table. In order to delete a single array we will create method called deleteArray(). This function will dynamically delete the targeted array. We use a function called splice() to remove the array data by setting the index position of that array.


The How to Delete Single Array of Data 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 Single Array of Data 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