Last updated Apr 13, 2021

Introduction to Filter method in javascript

Filter method in javascript

Introduction to Filter method in javascript

In this tutorial, we will learn about how to use and when to use the Filter method in JavaScript.

What is the Filter method?

The Filter() method is Used to filter for elements in the original array that satisfy a certain condition

Basically, Filter() method returns a new array containing the array elements that passed a specified test condition and leaves the original array untouched.

For Example,

Suppose we have an array as follows:

let arr = [300, 450, -400, 1000, -65, -13, 70, 300];

Now, we want's to get only positve value from it, so we can do this using filter as follows:

let modifiedArr =  arr.filter(function(num){
  return num > 0;
});

console.log(modifiedArr);//[300, 450, 1000, 70, 300]

How to Filter an Array of Objects?

Now suppose we have an array of objects as follows:

let users = [
    {name: "sam", country: "us"},
    {name: "steve", country: "uk"},
    {name: "john", country: "uk"},
    {name: "phillip", country: "india"}
];

Here, we want's to get list of users who belongs to uk, then we can filter as follows:

let filteredData = users.filter(function(ele){
    return ele.country=='uk'
})

console.log(filteredData);
//[{"name":"steve","country":"uk"},{"name":"john","country":"uk"}]

Conclusion:

🙏 Thanks for reading.

Do let me know If you face any difficulties please feel free to comment below we love to help you.

If you have any feedback suggestions then please inform us by commenting.

Don’t forget to share this tutorial with your friends on Facebook and Twitter

Tags

Available for projectsLet’s build together

Need this built properly,
not just explained?

I take on Angular and NestJS work directly — auth systems, multi-tenant SaaS, API rewrites and the deploy pipeline underneath them. You talk to the person writing the code.

Projects delivered
20+ Projects delivered
Angular • NestJS
7+ years Angular • NestJS
Average reply time
1 day Average reply time

What I can help with

  • Auth and permissions systems
  • Multi-tenant SaaS foundations
  • API design and rewrites
  • Deploy pipelines and VPS setup
  • Bug fixing and performance optimization
Send me a message

Tell me about your project and I’ll get back with a realistic scope and price.

Turn
Your Ideas
Into Reality

Stay in the loop

Get the finished tutorials, projects and resources in your inbox.

Join 5,000+ developers and get new tutorials, source code, product updates and exclusive content — straight to your inbox.

  • No spam
  • Useful content only
  • Unsubscribe anytime

Subscribe to TutsCoder

Be the first to get new tutorials, projects and tips.

Join 5,000+ developers already learning with us.

Learn
Build
Grow

Discussion

Comments

Share your thoughts, ask questions or suggest improvements. Let’s learn and grow together!

Loading comments…

Join the discussion

Sign in to join the discussion. Your account also keeps your saved tutorials and path progress.