Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
Filtering by several parameters at the same time

I need to implement user filtering according to the rules that come with the JSON file along with the users. There are two types of rules: "exclude" and "include" . They can come either individually or two at once. Right now, my code works well when two rules come in at once. But when there is only one rule, then the error in the console is Cannot read properties of undefined (reading 'forEach'). How can this problem be solved?

Users and rules:

{
  "data": [{
      "user": "user1",
      "city": "New York",
      "disabled": false
    },
    {
      "user": "user2",
      "city": "London",
      "disabled": false
    },
    {
      "user": "user3",
      "city": "Tokyo",
      "disabled": true
    }
  ],
  "condition": {
    "exclude": [{
      "disabled": true
    }],
    "include": [{
      "city": "New York",
    }],
  }
}

const data = responseJson.then((data) => {
  var filteredUsers = advancedFilter(data.data, data.condition);
  console.log(filteredUsers);
});

function advancedFilter(data, condition) {
  return data.filter((dataItem) => {
    var cond = true;
    if ("exclude" in condition || "include" in condition) {
      condition.exclude.forEach((condItem) => {
        for (var key in condItem) {
          cond = cond && dataItem[key] !== condItem[key];
        }
      });
      condition.include.forEach((condItem) => {
        for (var key in condItem) {
          cond = cond && dataItem[key] === condItem[key];
        }
      });
    }
    return cond;
  });
}

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You just need to make two if conditions separately, so if there is a exclude condition then run the forEach on exclude, Also if there is an include condition, then run the forEach on include.

The error happens because you loop throw both include and exclude even if one of them does not exist.

const responseJson = {
  "data": [{
      "user": "user1",
      "city": "New York",
      "disabled": false
    },
    {
      "user": "user2",
      "city": "London",
      "disabled": false
    },
    {
      "user": "user3",
      "city": "Tokyo",
      "disabled": true
    }
  ],
  "condition": {
    "exclude": [{
      "disabled": true
    }],
    "include": [{
      "city": "New York",
    }],
  }
}

var filteredUsers = advancedFilter(responseJson.data, responseJson.condition);
console.log(filteredUsers);

function advancedFilter(data, condition) {
  return data.filter((dataItem) => {
    var cond = true;
    if ("exclude" in condition) {
      condition.exclude.forEach((condItem) => {
        for (var key in condItem) {
          cond = cond && dataItem[key] !== condItem[key];
        }
      });
    }
    if ("include" in condition) {
      condition.include.forEach((condItem) => {
        for (var key in condItem) {
          cond = cond && dataItem[key] === condItem[key];
        }
      });
    }
    return cond;
  });
}

about 4 years ago · Santiago Trujillo Report

0

I'd recommend splitting out the logic into two if statements, like so:

if ("exclude" in condition) {
  condition.exclude.forEach((condItem) => {
    for (var key in condItem) {
      cond = cond && dataItem[key] !== condItem[key];
    }
  });
}
if ("include" in condition) {
  condition.include.forEach((condItem) => {
    for (var key in condItem) {
      cond = cond && dataItem[key] === condItem[key];
    }
  });
}

This way you won't be trying to read something undefined as you will already have checked that the property is present.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!