Home > Web Front-end > JS Tutorial > body text

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

DDD
Release: 2024-10-26 03:09:27
Original
705 people have browsed it

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

Filtering Array of Objects Based on Multiple Conditions in JavaScript

Problem:

Given an array of objects and a filter object, we aim to filter the array elements based on multiple conditions specified in the filter object. However, a current implementation encounters an issue where multiple elements meeting only a subset of conditions are included in the result.

Solution:

The issue arises from the incorrect usage of the for (var i = 0; i < filter.length; i ) loop within the filtering function. Since filter is an object, it does not have a length property, and this loop becomes redundant.

To address this, we can iterate directly over the properties of the filter object and check for property existence on each user object. The following code demonstrates the correct implementation:

<code class="javascript">function filterUsers(users, filter) {
  var result = [];
  for (var prop in filter) {
    if (filter.hasOwnProperty(prop)) {
      users.forEach(function(user) {
        if (user[prop] === filter[prop]) {
          result.push(user);
        }
      });
    }
  }
  return result;
}</code>
Copy after login

Improved Filter Function:

The improved filterUsers function now iterates over the users and checks each property of the filter object to see if it matches the corresponding property value of the user. Only users with matching values for all specified properties are included in the result.

Example:

<code class="javascript">var filter = {
  address: 'England',
  name: 'Mark',
};
var users = [{
  name: 'John',
  email: 'john@example.com',
  age: 25,
  address: 'USA',
},
{
  name: 'Tom',
  email: 'tom@example.com',
  age: 35,
  address: 'England',
},
{
  name: 'Mark',
  email: 'mark@example.com',
  age: 28,
  address: 'England',
},
];

var filteredUsers = filterUsers(users, filter);

console.log(filteredUsers);
// Output: [{
//   name: 'Mark',
//   email: 'mark@example.com',
//   age: 28,
//   address: 'England',
// }]</code>
Copy after login

This implementation ensures that only the user whose name is Mark and who lives in England is included in the result, as specified by the filter conditions.

The above is the detailed content of How to Filter an Array of Objects Based on Multiple Conditions in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!