JavaScript - Filter Array Key By Gender

In this tutorial we will create a Filter Array Key By Gender using JavaScript. This code will dynamically sort the array when the user click a button. The code use onclick() function to call a specific method that sort an array when the button is clickd and then it will run a conditional statement that organize the index position of an array by providing the gender string as a parameter in the sortArray();. You can apply this script to your code to make your transaction faster, it is a user-friendly program feel free to modify it. We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

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 - Filter Array Key By Gender</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-6">
  17. <button type="button" class="btn btn-primary" onclick="sortAscending()">Ascending</button> <button type="button" class="btn btn-primary" onclick="sortDescending()">Descending</button>
  18.  
  19. <br /><br />
  20.  
  21. <table class="table table-bordered">
  22. <thead class="alert-info">
  23. <tr>
  24. <td>Firstname</td>
  25. <td>Lastname</td>
  26. <td>Gender</td>
  27. </tr>
  28. </thead>
  29. <tbody id="result" style="background-color:#fff;"></tbody>
  30. </table>
  31. </div>
  32. </div>
  33. </body>
  34. <script src="js/script.js"></script>
  35. </html>

Creating the Script

This code contains the script of the application. This code will sort the array 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 members = [
  2. {"firstname":"John", "lastname":"Smith", "gender":"Male"},
  3. {"firstname":"Claire", "lastname":"Temple", "gender":"Female"},
  4. {"firstname":"Steve", "lastname":"Roger", "gender":"Male"},
  5. {"firstname":"Paul", "lastname":"Miles", "gender":"Male"},
  6. ];
  7.  
  8. displayData();
  9.  
  10. function displayData(){
  11. var data = "";
  12. if(members.length > 0){
  13. for(var i=0; i < members.length; i++){
  14. data += "<tr>";
  15. data += "<td>"+members[i].firstname +"</td>";
  16. data += "<td>"+members[i].lastname+"</td>";
  17. data += "<td>"+members[i].gender+"</td>";
  18. data += "</tr>";
  19. }
  20. }
  21.  
  22. document.getElementById('result').innerHTML = data;
  23. }
  24.  
  25.  
  26. function sortAscending(){
  27. if(members.length > 0){
  28. var sortArray = members;
  29.  
  30. sortArray.sort(function(a,b){
  31. if(a.lastname < b.lastname){
  32. return -1;
  33. }
  34.  
  35. if(a.lastname > b.lastname){
  36. return 1;
  37. }
  38.  
  39. return 0;
  40. });
  41.  
  42.  
  43. displayData();
  44.  
  45. }
  46.  
  47. }
  48.  
  49. function sortDescending(){
  50. if(members.length > 0){
  51. var sortArray = members;
  52.  
  53. sortArray.sort(function(a,b){
  54. if(a.lastname > b.lastname){
  55. return -1;
  56. }
  57.  
  58. else if(a.lastname < b.lastname){
  59. return 1;
  60. }else{
  61. return 0;
  62. }
  63.  
  64.  
  65. });
  66.  
  67. displayData();
  68.  
  69. }
  70. }
There you have it we successfully created a Filter Array Key By Gender 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