How to filter an array of objects in ES6 ?
Last Updated :
03 Jul, 2024
In this article, we will try to understand how we could filter out or separate certain data from the array of objects in ES6.
Let us first try to understand how we could create an array of objects by following certain syntax provided by JavaScript.
Javascript Array of Objects:
- The array of objects helps any user to store multiple values in a single variable.
- It actually stores a fixed-sized collection of sequentially accessed and that too of the same type.
- By using the following syntax we could easily make an array of multiple objects.
Syntax:
let array_of_objects = [
{ property-name: value},
{ property-name: value},
...
]
Let us look at the following example for a better understanding of how to create an array of objects.
Example: In this example, we will create a basic array in Javascript.
JavaScript
let people_details = [
{ name: "ABC", age: 18 },
{ name: "GeeksforGeeks", age: 30 },
{ name: "DEF", age: 50 },
];
console.log(people_details);
Output:
[
{ name: 'ABC', age: 18 },
{ name: 'GeeksforGeeks', age: 30 },
{ name: 'DEF', age: 50 }
]
Now that we have understood the concept of creating an array of objects, let us now see how we could filter out some data based on user requirements from the array of objects.
Javascript filter() Method
The following sequentially ordered points will depict our above-illustrated task:
- For filtering out contents from the array of objects we would be using at first the filter() method which we would be applying on our outermost array part.
- Inside that method, we would be passing a function that would execute on all of the objects present inside an array, and to access each of those objects we would be using an element of any name for the purpose of accessing those objects present inside that array.
- Then afterward based on the user's requirement the data from that array of objects will be filtered out and could be displayed if required.
Example 1: In this example, we will implement a basic filter() method which will filter the age of the object.
JavaScript
let people_details = [
{ name: "ABC", age: 18 },
{ name: "GeeksforGeeks", age: 30 },
{ name: "DEF", age: 50 },
];
console.log(people_details);
let data = people_details.filter(
element => element.age >= 30);
console.log(data);
Output:
[
{ name: 'ABC', age: 18 },
{ name: 'GeeksforGeeks', age: 30 },
{ name: 'DEF', age: 50 }
]
[
{ name: 'GeeksforGeeks', age: 30 },
{ name: 'DEF', age: 50 }
]
Example 2: In this example, we will use the filter() method for filtering out the blood group data which is required by the user as the end result. There also we will use && logical operator by providing both the conditions which is the age should be greater than 18 as well as the blood group required is B+.
JavaScript
let blood_groups_data = [
{ name: "ABC", age: 19, blood_group: "B+" },
{ name: "DEF", age: 20, blood_group: "AB+" },
{ name: "JOHN", age: 25, blood_group: "A+" },
{ name: "APPLE", age: 45, blood_group: "B+" },
{ name: "PETER", age: 48, blood_group: "AB-" },
{ name: "JAMES", age: 53, blood_group: "B+" },
];
console.log(blood_groups_data);
let blood_group_required_data = blood_groups_data.filter(
(person) => person.age > 18 && person.blood_group === "B+"
);
console.log(blood_group_required_data);
Output:
[
{ name: 'ABC', age: 19, blood_group: 'B+' },
{ name: 'DEF', age: 20, blood_group: 'AB+' },
{ name: 'JOHN', age: 25, blood_group: 'A+' },
{ name: 'APPLE', age: 45, blood_group: 'B+' },
{ name: 'PETER', age: 48, blood_group: 'AB-' },
{ name: 'JAMES', age: 53, blood_group: 'B+' }
]
[
{ name: 'ABC', age: 19, blood_group: 'B+' },
{ name: 'APPLE', age: 45, blood_group: 'B+' },
{ name: 'JAMES', age: 53, blood_group: 'B+' }
]
Using reduce method
The code uses reduce() to iterate through an array of objects (people). It checks each object's age property and accumulates objects with age < 35 into filteredPeople, demonstrating filtering without using filter() directly.
Example:
JavaScript
// Example array of objects
const people = [
{ name: 'Alice', age: 30 },
{ name: 'Bob', age: 25 },
{ name: 'Charlie', age: 35 },
{ name: 'David', age: 40 }
];
// Filtering without using filter()
const filteredPeople = people.reduce((acc, person) => {
// Add person to accumulator if condition is met
if (person.age < 35) {
acc.push(person);
}
return acc;
}, []);
console.log(filteredPeople);
Output[ { name: 'Alice', age: 30 }, { name: 'Bob', age: 25 } ]
Similar Reads
How to Filter an Array in JavaScript ?
The array.filter() method is used to filter array in JavaScript. The filter() method iterates and check every element for the given condition and returns a new array with the filtered output.Syntaxconst filteredArray = array.filter( callbackFunction ( element [, index [, array]])[, thisArg]);Note: I
2 min read
How to filter by object property in AngularJS?
Filtering by object property in AngularJS is the concept of choosing the specific objects from the data array which is based on the individual properties. In creating a web application, this is the most common task that deals with the data that needs to be sorted and then displayed to the user. Deve
6 min read
How to Filter Object Array based on Attributes?
One can use the filter() and reduce() functions in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array. The filter() funct
3 min read
How to use array of objects in EJS Template Engine ?
Arrays of objects are collections of JavaScript objects stored within an array. Each object within the array can contain multiple key-value pairs, making them versatile for organizing various types of data. For example, you might have an array of objects representing users, products, or posts in a s
3 min read
How to Convert an Array of Objects into an Array of Arrays ?
An Array of objects is an array that contains multiple objects as its elements which can be converted into an array of arrays i.e. an array that contains multiple arrays as its elements. There are several methods listed below to achieve this task. Table of Content Using Object.keys() and Object.valu
3 min read
How to apply filters to *ngFor in Angular ?
In this article, we will see How to apply filters to *ngFor in AngularJS, along with understanding their basic implementation through the examples. NgFor is used as a Structural Directive that renders each element for the given collection each element can be displayed on the page. Implementing the f
3 min read
How to Convert Object to Array in Lodash ?
Converting an Object to an Array consists of changing the data structure from key-value pairs to an array format. Below are the different approaches to converting objects to arrays in Lodash: Table of Content Using toArray function Using values functionRun the below command before running the below
2 min read
How to Remove Duplicates from an Array of Objects in JavaScript?
Here are some effective methods to remove duplicates from an array of objects in JavaScript1. Using filter() and findIndex() Methods - Most UsedThe simplest way to remove duplicates is by using filter() and findIndex(). This method keeps the first occurrence of each object with a unique property (li
3 min read
How to Access Array of Objects in JavaScript ?
Accessing an array of objects in JavaScript is a common task that involves retrieving and manipulating data stored within each object. This is essential when working with structured data, allowing developers to easily extract, update, or process information from multiple objects within an array.How
4 min read
How to compare Arrays of Objects in JavaScript?
In JavaScript, comparing arrays of objects can be more complex than comparing primitive data types. We will discuss different ways to compare arrays of objects effectively, with detailed code examples and explanations.Syntax: Before going to detail the comparison techniques, let's first understand h
5 min read