JavaScript - Get Last Data from an Array

Submitted by: 
Visitors have accessed this post 141 times.

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

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="<a href="">Sourcecodester</a>
  10. " rel="nofollow">">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Get Array Last Data</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-5">
  18. <button class="btn btn-primary" id="get">Get Last Data</button>
  19. <br /><br />
  20. <div id="display"></div>
  21. </div>
  22. <div class="col-md-1"></div>
  23. <div class="col-md-6">
  24. <table class="table table-bordered">
  25. <tr>
  26. <th>List of array data</th>
  27. </tr>
  28. </thead>
  29. <tbody id="result">
  30. </tbody>
  31. </table>
  32. </div>
  33. </div>
  34. <script src="js/script.js"></script>
  35. </body>
  36. </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"];
  3. displayData(array);
  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. }
  13. document.getElementById('result').innerHTML=html;
  14. }
  17. document.getElementById("get").addEventListener("click", getLastData.bind(this, array));
  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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.