How to Filter Array of Objects in Javascript by Any Property

How to Filter Array of Objects in Javascript by Any Property

Creating a function for filtering by property
Ferenc Almasi β€’ 2022 June 20 β€’ Read time 3 min read
  • twitter
  • facebook
JavaScript

To filter an array of objects in JavaScript by any property, we can use the Array.filter method in the following way:

Copied to clipboard! Playground
const users = [
  { id: 1, name: 'John', isAdmin: false },
  { id: 2, name: 'Jane', isAdmin: true  },
  { id: 3, name: 'Joel', isAdmin: false }
]

users.filter(user => user.isAdmin)

// Returns
-> [{ id: 2, name: 'Jane', isAdmin: true }]
Using filter to filter array of objects based on a property

We can call this method on arrays, and it expects a callback function. The filter method runs a test in the callback function. Elements that pass the test are added to a new array. This means that the filter method will always return a new array, and does not modify the original array.

In the above code example, we try to filter for users whose isAdmin property is set to true. This is equivalent to returning a value from the callback function:

Copied to clipboard!
users.filter(user => {
    return user.isAdmin
})
This is equivalent to the above code example

The filter method always expects a true or false value to be returned. If true is returned, the test is passed and the item is added to a new array. If false is returned, it will not be added to the new array.

We can of course not only do this with boolean values but with any other value as well. Imagine you have a list of files with their sizes, and you want to filter out large files. You could do the following, where you compare the size property to a fixed value:

Copied to clipboard! Playground
const files = [
  { id: 1, name: 'html.md', size: 499 },
  { id: 2, name: 'css.md', size: 612  },
  { id: 3, name: 'javascript.md', size: 1236 }
]

const largeFiles = files.filter(file => file.size > 1000)
Note that the name of the item in the callback function can be anything

We can also abstract the filter method into a function that we can reuse to filter by any boolean property. We can achieve this with a single line of code:

Copied to clipboard! Playground
const filter = (array, property) => array.filter(item => item[property])

// Then call it later like so:
filter(users, 'isAdmin')

// Returns
-> [{ id: 2, name: 'Jane', isAdmin: true }]

This function expects an array to be filtered, and the property that we are looking for, then immediately returns a new array using filter. Inside the callback of the filter, we can pass the passed property using bracket notation. Note that the above function will only work with boolean properties. For more complex filters, you may want to write out the callback function.

Another thing to note about the filter method is that it also accepts more parameters, namely the index and the array itself if needed. These are optional parameters, so you don't need to define them if you are not going to use them.

Copied to clipboard! Playground
users.filter((user, index, array) => {
    console.log(user)  // Logs out the current element in the loop
    console.log(index) // Logs out the current index in the loop, starting from 0
    console.log(array) // Logs out the array itself

    return user.isAdmin
})
Using index and array as optional parameters in filter

Want to know how to sort arrays too? Check out the following tutorial:

How to Sort Array of Objects in JavaScript
  • twitter
  • facebook
JavaScript
Did you find this page helpful?
πŸ“š More Webtips
Mentoring

Rocket Launch Your Career

Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies:

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.