Filter An Array With Multiple Conditions In JavaScript

To filter an array with multiple conditions in JavaScript, you can use the filter() method. The filter() method accepts a callback function that will be called for each element in the array. Use the && condition inside the callback function to check for multiple conditions.

filter an array with multiple conditions in javascript

Filter An Array With Multiple Conditions In JavaScript

If you want to filter an array with multiple conditions in JavaScript, you can use the && condition inside the callback function of the filter() method. The code below will return a new array that contains only elements that have a value of 30 or higher and less than 50.

var arr = [-5, 10, 15, 20, 25, 30, 35, 40, 45, 50];

var newArr = arr.filter(function(elem) {

  return elem >= 30 && elem < 50;

});

console.log(newArr);

// [30, 35, 40, 45]

Let’s take an example of an array of objects. To filter the array of objects with multiple conditions, you can use the && condition inside the callback function of the filter() method. The code below will return a new array that contains only elements that have a value of 30 or higher and less than 50 for the key “age”.

var arr = [{age: 15, name: "john"},

{age: 25, name: "mike"},

{age: 35, name: "ben"}];

var newArr = arr.filter(function(elem) {

return elem.age >= 30 && elem.age < 50;

});

console.log(newArr);

// [{age: 35, name: "ben"}]

In the above examples, we used the Array.filter() method with the && condition to filter arrays with multiple conditions.

When you use the && condition, all the conditions must be true for the element to be included in the new array. If one of the conditions is false, the element will not be included in the new array.

You can also use the || condition (or) to filter arrays with multiple conditions where any one of the condition is met.

The code below will return a new array that contains only elements that have values of 10, 20, or 30.

var arr = [-5, 10, 15, 20, 25, 30, 35, 40, 45, 50];

var newArr = arr.filter(function(elem) {

  return elem === 10 || elem === 20 || elem === 30;

});

console.log(newArr);

//  ["10", "20", "30"]

You can also use the || condition (or) to filter an array of objects with multiple conditions.

The code below will return a new array that contains only elements that have values of 10, 20, or 30 for the key “value”.

var arr = [ {name: "john", value: 10},

{name: "mike", value: 25},

{name: "ben", value: 35}];

var newArr = arr.filter(function(elem) {

  return elem.value === 10 || elem.value === 20 || elem.value === 30;

});

console.log(newArr);

// [{name: "john", value: 10}]

Conclusion

In this tutorial, we learned how to filter an array with multiple conditions in JavaScript using the && (and) and || (or) operators. We also learned how to use the Array.filter() method with multiple conditions.

We hope you found this tutorial helpful. If you have any questions or comments, please feel free to leave them below in the comments section.

Happy Coding! 🙂

Leave a Reply