Filter an Array with Multiple Conditions in JavaScript
Filter an Array with Multiple Conditions using &&
(And) operator
To filter an array with multiple conditions:
- Use the
Array.filter()
method on the array. - Apply the
&&
(And) operator to check for multiple conditions. - The
Array.filter()
method will return an array of all elements that satisfy the conditions.
Example –
const people = [
{name: 'Matthew', age: 30},
{name: 'Bob', age: 40},
{name: 'Weston', age: 30},
];
const results = people.filter(element => {
return element.age === 30 && element.name === 'Weston';
});
console.log(results);
Here’s what the above code is doing:
- We have an array of objects called
people
. - We call the
filter()
method on the people array. - We pass in a callback function to the
filter()
method. - The callback function returns
true
if the element’s age is 30 and the element’s name isWeston
. - The
filter()
method returns a new array with the elements that returnedtrue
in the callback function.
The function we passed to the Array.filter()
method is invoked with each element in the array.
If the function returns a value that is truthy, the element from the array gets added to the results array.
The element will only be added to the filtered array if both conditions are met.
If the callback function never returns a truthy value, then Array.filter()
will return an empty array.
The Array.filter()
method can be used with the || (or) operator to filter an array with multiple conditions, where only one condition has to be satisfied. This will return a new array with only the elements that satisfy at least one condition.
Filter an Array with Multiple Conditions using ||
(or) operator
To filter an array with multiple conditions:
- Call the
Array.filter()
method on the array. - Use
||
(or) operator to check if only one of two conditions has to be satisfied. - The
Array.filter()
method will return an array of all elements that satisfy the conditions.
Example –
const people = [
{name: 'Matthew', age: 30},
{name: 'Bob', age: 40},
{name: 'Weston', age: 30},
];
const results = people.filter(element => {
// using OR (||) operator
return element.age === 40 || element.name === 'Weston';
});
console.log(results);
or
operator.Here’s what the logic in the code:
- We have an array of objects called people.
- We call the
Array.filter()
method on the people array. - We pass in a callback function to the
Array.filter()
method. - The callback function takes in an element.
- The callback function returns return the element if the element’s age is
40
OR the element’s name isWeston
. - The filter method returns a new array with the elements that return
true
.
We checked for multiple conditions using the OR ||
operator.
The results array will include elements for which either of the conditions returns a truthy value.
In the code sample, we check for objects with the property age of 40 or the property name of Weston.
The Array.filter()
method returns elements that satisfy at least one of the conditions. In this case, there are two elements in the array that satisfy at least one of the two conditions, so the Array.filter method returns both of them.