How to useArray.from() and filter() in Javascript

The combination of Array.from() and filter() allows for a concise method of filtering based on multiple properties. we will filter the array by the use of the filter() method and then we will make an array of those values by the use of the Array.from() method.

Example: This example shows the use of the above approach.

JavaScript




// Sample array of objects
const data = [
    { id: 1, name: 'John', age: 25 },
    { id: 2, name: 'Jane', age: 30 },
    { id: 3, name: 'Doe', age: 25 },
];
 
// Filtering based on multiple properties
// using Array.from() and filter()
const filteredData = Array.from(data).filter(item =>
    item.age === 25 && item.name === 'John');
console.log(filteredData);


Output

[ { id: 1, name: 'John', age: 25 } ]

How to Filter an Array of Objects Based on Multiple Properties in JavaScript ?

Filtering an array of objects based on multiple properties is a common task in JavaScript. It allows us to selectively extract items from an array that satisfy specific conditions. We will explore different approaches to achieve this task.

These are the following approaches:

Table of Content

  • Using the filter() Method
  • Using a for Loop
  • Using Array.from() and filter()
  • Using reduce() method
  • Using forEach() and filter()
  • Using Array.filter() and Object.entries()
  • Using Array.filter() and Object.keys()

Similar Reads

Approach 1: Using the filter() Method

The filter() method is a built-in array method in JavaScript that creates a new array with elements that pass the provided function’s test....

Approach 2: Using a for Loop

...

Approach 3: Using Array.from() and filter()

A traditional for loop allows explicit control over the filtering process, iterating through each object and applying the specified conditions. we will be using the for loop and by using if else we will check for the condition and add the true values into the newly formed array....

Approach 4: Using reduce() method

...

Approach 5: Using forEach() and filter()

The combination of Array.from() and filter() allows for a concise method of filtering based on multiple properties. we will filter the array by the use of the filter() method and then we will make an array of those values by the use of the Array.from() method....

Approach 6: Using Array.filter() and Object.entries()

...

Approach 7: Using Array.filter() and Object.keys()

The reduce() method can be employed to iteratively build an array of objects that meet the specified conditions. we will reduce the original array by filtering it to according to the our requirement....