JavaScript Filter Object: How to Filter Objects in Array
If you have the array of objects and you have to filter out the objects then use the filter() method that accepts a test function, and you need to write that test method in such a way that, it will filter out the unnecessary objects and give the array of objects that pass our test. The filter() method is used in the JavaScript filter object. We can use the filter() method when we are dealing with an array of objects.
JavaScript Filter Object
To filter an array of objects in JavaScript, use the Javascript filter() method. The filter() creates a new array with elements that fall under given criteria from the existing array. The standard use case of .filter() is with an array of objects through their properties.
Syntax
array.filter(function(currentValue, index, array), thisValue)
Parameters
function(currentValue, index, array)
The function is required, and it is the function to be run for each element in the array.
Function arguments
Argument | Description |
---|---|
currentValue | Required. The value of the current element |
index | It is an optional. The array index of the current item |
array | It is an optional. The array object the current item belongs to |
thisValue
It is an optional parameter. A value to be passed to the function to be used as its “this” value. If this parameter is empty, the value “undefined” will be given as its “this” value.
Return Value
The filter() function returns an array containing all the array elements that pass the test. If no items pass the test, it returns an empty array.
Example
// app.js let actors = [ { name: 'Eleven', show: 'Stranger Things' }, { name: 'Jonas', show: 'Dark' }, { name: 'Mike', show: 'Stranger Things' }, { name: 'Martha', show: 'Dark' }, { name: 'Dustin', show: 'Stranger Things' }, ] STActors = actors.filter(actor => actor.show == 'Stranger Things'); console.log(STActors);
Output
[ { name: 'Eleven', show: 'Stranger Things' }, { name: 'Mike', show: 'Stranger Things' }, { name: 'Dustin', show: 'Stranger Things' } ]
In this example, we defined an array with five objects, and it has two shows.
- Stranger Things
- Dark
Now, we want to get the only objects whose show is Stranger Things. To do that, we need to use an array filter() function to fetch the only objects whose show is Stranger Things.
The filter() function returns the new array containing only Stranger Things show.
So, we are comparing the object’s properties show with Stranger Things name, and if it found a match, then it will put into the new array which we will get in the end.
In the array filter() method, if the current item passes the condition, then it will put into a new array, and that array will be returned finally by the filter() method.
Conclusion
JavaScript array filter() method accepts a test function and returns the new array containing only the items of the original array that pass the test provided.
If we want to filter objects from the array, then you have to write a test function that compares the object’s property values, and based on that, it will filter out the objects and returns the array of objects that satisfy your requirements.
That is it for JavaScript filter object example.