Sometimes, we want to filter array of objects whose any properties contains a value with JavaScript.
In this article, we’ll look at how to filter array of objects whose any properties contains a value with JavaScript.
How to filter array of objects whose any properties contains a value with JavaScript?
To filter array of objects whose any properties contains a value with JavaScript, we use the array filter
method.
For instance, we write
const filterByValue = (array, value) => {
return array.filter(
(data) =>
JSON.stringify(data).toLowerCase().indexOf(value.toLowerCase()) !== -1
);
};
to call array.filter
with a callback that checks if the stringified version of the data
object in array
has the value
we’re looking for indexOf
.
If indexOf
returns something other than -1, then the array returned by filter
includes the data
object.
Conclusion
To filter array of objects whose any properties contains a value with JavaScript, we use the array filter
method.