Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda