Skip to content
the poor coder

javascript filter array based on attributes

Poor Man
Published
Reading time
2 min read
On this page

How to Filter an Array Based on Attributes using JavaScript#

Filtering arrays based on attributes is a common task in JavaScript programming. The filter() method is used to create a new array with all elements that pass the test implemented by the provided function. The filter() method is very useful for filtering arrays based on certain attributes.

Using the filter() method#

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

Text

var persons = [
   {name: 'John', age: 25},
   {name: 'Peter', age: 30},
   {name: 'Mary', age: 20}
];

var filteredPersons = persons.filter(function(person) {
   return person.age > 25;
});

console.log(filteredPersons); // Output: [{name: 'Peter', age: 30}]

In the above example, we have an array of persons with their respective names and ages. We want to filter the array based on the age attribute. So, we use the filter() method and pass a function as its argument. The function returns true if the age attribute of a person is greater than 25. The filter() method creates a new array with all elements that pass the test implemented by the provided function. In this case, we get only one person object with name Peter and age 30.

Using the ES6 Arrow Function#

The ES6 arrow function provides a more concise way to write filter functions. Here's how we can use it to filter an array based on attributes:

Text

var persons = [
   {name: 'John', age: 25},
   {name: 'Peter', age: 30},
   {name: 'Mary', age: 20}
];

var filteredPersons = persons.filter(person => person.age > 25);

console.log(filteredPersons); // Output: [{name: 'Peter', age: 30}]

In this example, we use the ES6 arrow function to write the filter function. The syntax is much more concise and easier to read. The filter() method creates a new array with all elements that pass the test implemented by the provided arrow function. In this case, we get only one person object with name Peter and age 30.

Using the Lodash Library#

The Lodash library provides a very powerful set of functions for working with arrays and objects in JavaScript. Here's how we can use Lodash to filter an array based on attributes:

Text

var persons = [
   {name: 'John', age: 25},
   {name: 'Peter', age: 30},
   {name: 'Mary', age: 20}
];

var filteredPersons = _.filter(persons, function(person) {
   return person.age > 25;
});

console.log(filteredPersons); // Output: [{name: 'Peter', age: 30}]

In this example, we use the _.filter() function from the Lodash library to filter the array based on attributes. The _.filter() function takes two arguments, the array to be filtered and a function that returns true if an element should be included in the filtered array. In this case, we get only one person object with name Peter and age 30.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC