javascript filter array based on attributes

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.


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:


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:


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.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe