JavaScript - Get Last Data from an Array

In this tutorial we will create a Get Last Data from an Array using JavaScript. This code will retrieve the last data of an array when the user click the button. The code use onclick() to initiate a method for slicing the array data by adding a parameter [array.length - 1] in the index position. This program is a user-friendly. Feel free to use it in your system. We will use JavaScript to allow you to implement complex things on web pages. It enables you to create dynamically updating content and add some interactive visual for the user transactions.

Getting Started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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">JavaScript - Get Array Last Data</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-5">
  17. <button class="btn btn-primary" id="get">Get Last Data</button>
  18. <br /><br />
  19. <div id="display"></div>
  20. </div>
  21. <div class="col-md-1"></div>
  22. <div class="col-md-6">
  23. <table class="table table-bordered">
  24. <tr>
  25. <th>List of array data</th>
  26. </tr>
  27. </thead>
  28. <tbody id="result">
  29. </tbody>
  30. </table>
  31. </div>
  32. </div>
  33. <script src="js/script.js"></script>
  34. </body>
  35. </html>

Creating the Script

This code contains the script of the application. This code will get the last array key value when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. var array = ["John", "Karl", "Steve", "Natasha", "Sam"];
  2.  
  3. displayData(array);
  4.  
  5. function displayData(array){
  6. var html="";
  7. for(var i=0; i<array.length; i++){
  8. html +="<tr>";
  9. html +="<td>"+array[i]+"</td>";
  10. html +="</tr>";
  11. }
  12.  
  13. document.getElementById('result').innerHTML=html;
  14. }
  15.  
  16.  
  17. document.getElementById("get").addEventListener("click", getLastData.bind(this, array));
  18.  
  19.  
  20. function getLastData(array){
  21. var data = array[array.length - 1];
  22. document.getElementById('display').innerHTML="<center><label>The last data of an array is: <span class='text-primary'>"+data+"</span></label></center>";
  23. }
There you have it we successfully created a Get Array Last Data using 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!

Add new comment