How to Filter Object Array Based on Attributes in JavaScript

In this tutorial we will create a How to Filter Object Array Based on Attributes. This tutorial purpose is to provide more useful function that can be used in array data. This will tackle the filtering data attributes of array object. I will provide a sample program to show the actual coding of this tutorial. So Let's do the coding.

What is JavaScript Array?

Arrays are a special type of objects where typeof operator in JavaScript returns "object" for array. It is a container object that holds a fixed number of values of a single type. It also enables storing a collection of multiple items under a single variable name.

Before we get started:

This 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 display an interface that will list the array object in the HTML page. 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"/>
  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://www.sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="container-fluid">
  13. <div class="row">
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">How to Filter Object Array Based on Attributes</h3>
  17. <hr style="border-top: 1px SOLID #8c8b8b;"/>
  18. <pre id="display"></pre>
  19. <h6><strong>Note:</strong> Will filter the price if less than the display list<h5>
  20. <input type="text" id="price" placeholder="Enter price"/><button onclick="filter();">Filter</button> <button onclick="reset();">Reset</button>
  21. </div>
  22. </div>
  23. </div>
  24. </body>
  25. <script src="script.js"></script>
  26. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically filter the array object base on the given attribute conditional method. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. var array = {
  2. 'mobile': [{
  3. "mobile_id": "1",
  4. "price": "600",
  5. "brand": "samsung",
  6. "model": "Samsung S22",
  7. }, {
  8. "mobile_id": "2",
  9. "price": "750",
  10. "brand": "apple",
  11. "model": "Iphone 14",
  12. },{
  13. "mobile_id": "3",
  14. "price": "850",
  15. "brand": "samsung",
  16. "model": "Galaxy Z Fold 4",
  17. },{
  18. "mobile_id": "4",
  19. "price": "450",
  20. "brand": "vivo",
  21. "model": "Vivo V23",
  22. },
  23. ]
  24. };
  25.  
  26. function reset(){
  27. document.getElementById("display").innerHTML=JSON.stringify(array, null, 2);
  28. }
  29.  
  30.  
  31. function filter(){
  32. let price=document.getElementById("price").value;
  33.  
  34. if(typeof price !== 'undefined' && price){
  35. let newArray = array.mobile.filter(function (el) {
  36. return el.price >= price
  37. });
  38.  
  39. document.getElementById("display").innerHTML=JSON.stringify(newArray, null, 2);
  40. }else{
  41. alert("Please enter price first!");
  42. }
  43. }
  44. document.getElementById("display").innerHTML=JSON.stringify(array, null, 2);

In this code we will display first the array object using the JSON.stringify() function into the html page. To filter the array object we will first bind the button and input text then we use the filter() function that will basically outputs all the element object that pass a specific test or satisfies a specific function. In my example I filter out the price data where the value is greater than the listed value.

Output:

The How to Filter Object Array Based on Attributes 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 Object Array Based on Attributes 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