How to Filter Data Base on the Key Value in JavaScript

How to Filter Data Base on the Key Value in JavaScript

Introduction

In this tutorial we will create a How to Filter Data Base on the Key Value in JavaScript. This tutorial purpose is to teach you how you can filter a data base on the key value. This will tackle all the important functionality that will filter your data. 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 wan to filter any data I will give my best to provide you the easiest way of creating this program Filter Data in the Table. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the html table and buttons for triggering an event. 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="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">How to Filter Data Base on  the Key Value</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-8">
  17.                         <h4>Filter by Lastname</h4>
  18.                         <button type="button" class="btn btn-primary" onclick="sortAscending()">Ascending</button> <button type="button" class="btn btn-primary" onclick="sortDescending()">Descending</button>
  19.                         <br /><br />
  20.                         <table class="table table-bordered">
  21.                                 <thead class="alert-info">
  22.                                         <tr>
  23.                                                 <td>Firstname</td>
  24.                                                 <td>Lastname</td>
  25.                                                 <td>Gender</td>
  26.                                                 <td>Address</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="script.js"></script>
  35. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will allow to filter a data when you clicked 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 = [
  2.         {"firstname":"John", "lastname":"Smith", "gender":"Male", "address": "New York"},
  3.         {"firstname":"Claire", "lastname":"Temple", "gender":"Female", "address": "Racoon City"},
  4.         {"firstname":"Jake", "lastname":"Blake", "gender":"Male", "address": "New Jersey"},
  5.         {"firstname":"Harry", "lastname":"Patkinson", "gender":"Male", "address": "Canada"},
  6.         {"firstname":"Stephanie", "lastname":"Miles", "gender":"Female", "address": "Canada"}
  7. ];
  8.  
  9. displayData(); 
  10.  
  11. function displayData(){
  12.         var data = "";
  13.         if(members.length > 0){
  14.                 for(var i=0; i < members.length; i++){
  15.                         data += "<tr>";
  16.                         data += "<td>"+members[i].firstname +"</td>";
  17.                         data += "<td>"+members[i].lastname+"</td>";
  18.                         data += "<td>"+members[i].gender+"</td>";
  19.                         data += "<td>"+members[i].address+"</td>";
  20.                         data += "</tr>";
  21.                 }
  22.         }
  23.  
  24.         document.getElementById('result').innerHTML = data;
  25. }
  26.  
  27.  
  28. function sortAscending(){
  29.         if(members.length > 0){
  30.                 var sortArray = members;
  31.  
  32.                 sortArray.sort(function(a,b){
  33.                         if(a.lastname < b.lastname){
  34.                                 return -1;
  35.                         }
  36.  
  37.                         if(a.lastname > b.lastname){
  38.                                 return 1;
  39.                         }
  40.  
  41.                         return 0;
  42.                 });
  43.  
  44.  
  45.                 displayData();
  46.  
  47.         }
  48.  
  49. }
  50.  
  51. function sortDescending(){
  52.         if(members.length > 0){
  53.                 var sortArray = members;
  54.  
  55.                 sortArray.sort(function(a,b){
  56.                         if(a.lastname > b.lastname){
  57.                                 return -1;
  58.                         }
  59.  
  60.                         else if(a.lastname < b.lastname){
  61.                                 return 1;
  62.                         }else{
  63.                                 return 0;
  64.                         }
  65.  
  66.  
  67.                 });
  68.  
  69.                 displayData();
  70.  
  71.         }
  72. }

In the code above we just simply create a method that will your data base on the key value called sortAscending() and sortDescending(). This function will sort your base on your set key value, in my case I set the key value with lastname. As you can see whenever I click the filter button it will automatically filter the data.

Output:

The How to Filter Data Base on the Key Value 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 Filter Data Base on the Key Value 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