# JavaScript, how to filter an array

> Learn how to filter an array in JavaScript using the built-in filter() method with a callback, which returns a new array containing only the items that match.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2020-11-14 | Topics: [JavaScript](https://flaviocopes.com/tags/js/) | Canonical: https://flaviocopes.com/how-to-filter-array-javascript/

You have an array, and you want to filter it to get a new array with just some of the values of the original array.

How can you do so?

[JavaScript](https://flaviocopes.com/javascript/) arrays come with a built-in `filter()` method that we can use for this task.

Say we have an array with 4 objects representing 4 dogs:

```js
const dogs = [
  {
    name: 'Roger',
    gender: 'male'
  },
  {
    name: 'Syd',
    gender: 'male'
  },
  {
    name: 'Vanille',
    gender: 'female'
  },
  {
    name: 'Luna',
    gender: 'female'
  }
]
```

and you want to filter the male dogs only.

You can do so in this way:

```js
const maleDogs = dogs.filter((dog) => dog.gender === 'male')

// [ { name: 'Roger', gender: 'male' }, { name: 'Syd', gender: 'male' } ]
```
