Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

113
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda