How to Remove Element from an Array in JavaScript

How to Remove Element from an Array in JavaScript


In this tutorial we will create a How to Remove Element from an Array in JavaScript. This tutorial purpose is to teach you how you can remove an array dynamically. This will tackle all the basic function that will remove 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 if you remove an array data. I will give my best to provide you the easiest way of creating this program Remove an 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

Creating The Interface

This is where we will create a simple interface for our application. This code will display html table and remove 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="">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 Remove Element from an Array in JavaScript</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <table class="table table-bordered">
  17. <thead class="alert-info">
  18. <tr>
  19. <th>Array</th>
  20. <th></th>
  21. </tr>
  22. </thead>
  23. <tbody id="result"></tbody>
  24. </table>
  25. </div>
  26. </body>
  27. <script src="script.js"></script>
  28. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically remove an 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. let array = ["Chris Pratt", "John Smith", "Neil Armstrong", "Stephen Hawkins", "Steve Jobs", "Michael Douglas"];
  3. arrayList();
  5. function removeArray(index){
  6. alert(array[index] + " have been removed");
  7. array.splice(index, 1);
  8. arrayList();
  9. }
  11. function arrayList(){
  12. let table = "" ;
  14. for(var i=0; i < array.length; i++){
  15. table += "<tr>";
  16. table += "<td>" + array[i] +"</td>"
  17. + "<td><button class='btn btn-danger' onclick='removeArray("+i+")'>Remove</button></td>";
  18. table += "</tr>";
  20. }
  22. document.getElementById("result").innerHTML = table;
  23. }

In the code above we create a method that will display the list of array in html table called arrayList(). This function is consist of appending of array of data to html table using the innerHTMLfunction. We will now create a method that will remove our array from the list called removeArray(). This function use splice function to remove the array from a list based on the index positon.


The How to Remove 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 Remove 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