Javascript Filter Array Elements with Multiple Conditions

Share this Article
Reading Time: 4 minutes
459 Views

Inside this article we will see the concept i.e Javascript Filter Array Elements with Multiple Conditions. Article contains the classified information about How to filter javascript array by multiple conditions.

If you are looking for a solution i.e How to Filter Array with Multiple Conditions in JavaScript then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Learn More –

Let’s get started.

Example #1: Array Filter with Multiple Conditions – Logical OR (||)

Here,

We will use the concept of Javascript i.e Filter an Array with Multiple Conditions in JavaScript

Usage of Logical OR with javascript array filter method.

Syntax

const data = arr.filter(function (element) {

  return // condition1 || condition2 || condition3...

});

Example

//...

const users = [
  {name: 'Sanjay', age: 32},
  {name: 'Vijay', age: 30},
  {name: 'Ashish', age: 20},
  {name: 'Sunil', age: 38},
  {name: 'Suresh', age: 20},
];

const results = users.filter(user => {
  // using OR (||) operator
  return user.age > 30 || user.name.startsWith('S');
});

console.log(results);

//...

Concept to filter array using these conditions – Either user has greater than 30 OR username starts with S

const results = users.filter(user => {
  // using OR (||) operator
  return user.age > 30 || user.name.startsWith('S');
});

Console Output

Example #2: Array Filter with Multiple Conditions – Logical AND (&&)

Here,

We will use the concept of Javascript i.e Filter an Array with Multiple Conditions in JavaScript

Usage of Logical AND with javascript array filter method.

Syntax

const data = arr.filter(function (element) {

  return // condition1 && condition2 && condition3...

});

Example

//...

const users = [
  {name: 'Sanjay', age: 32},
  {name: 'Vijay', age: 30},
  {name: 'Ashish', age: 20},
  {name: 'Sunil', age: 38},
  {name: 'Suresh', age: 20},
];

const results = users.filter(user => {
  // using AND (&&) operator
  return user.age > 30 && user.name.startsWith('S');
});

console.log(results);

//...

Concept to filter array using these – Both conditions user has greater than 30 AND username starts with S

const results = users.filter(user => {
  // using OR (||) operator
  return user.age > 30 && user.name.startsWith('S');
});

Console Output

We hope this article helped you to learn Javascript Filter Array Elements with Multiple Conditions Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5