userName
userName

Reputation: 945

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;
  });
}

Upvotes: 1

Views: 31

Answers (2)

amyap
amyap

Reputation: 11

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.

Upvotes: 1

Mina
Mina

Reputation: 17119

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;
  });
}

Upvotes: 1

Related Questions