This is the link for the template that i used for the layout design https://getbootstrap.com/.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <link rel="stylesheet" type="text/css" href="css/bootstrap.css" /> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> </div> </nav> <div class="container-fluid"> <div class="row"> <div class="col-md-6 well"> <hr style="border-top: 1px SOLID #8c8b8b;"/> </div> </div> </div> </body> </html>
var array = { 'mobile': [{ "mobile_id": "1", "price": "600", "brand": "samsung", "model": "Samsung S22", }, { "mobile_id": "2", "price": "750", "brand": "apple", "model": "Iphone 14", },{ "mobile_id": "3", "price": "850", "brand": "samsung", "model": "Galaxy Z Fold 4", },{ "mobile_id": "4", "price": "450", "brand": "vivo", "model": "Vivo V23", }, ] }; function reset(){ document.getElementById("display").innerHTML=JSON.stringify(array, null, 2); } function filter(){ let price=document.getElementById("price").value; if(typeof price !== 'undefined' && price){ let newArray = array.mobile.filter(function (el) { return el.price >= price }); document.getElementById("display").innerHTML=JSON.stringify(newArray, null, 2); }else{ alert("Please enter price first!"); } } 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.
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