Shortest Article on Javascript Filter Method
The filter method got a single function and inside it, it got a single parameter( this parameter contains all the items from the array ).
The filter method always returns a new Array and it doesn't modify the original array.
Check the below code snippet, we have an array of objects containing a different types of mobiles and their price.
Here, We are checking and filtering those mobile phones priced above 800.
const mobile = [
{
name: "iphone",
price: 999,
},
{
name: "s23 ultra",
price: 899,
},
{
name: "vivo",
price: 799,
},
{
name: "oneplus",
price: 850,
},
];
const filteredArr = mobile.filter((item) => item.price > 800);
console.log(filteredArr);
//output
[
{
"name": "iphone",
"price": 999
},
{
"name": "s23 ultra",
"price": 899
},
{
"name": "oneplus",
"price": 850
}
]