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

373
Vistas
Dynamic if conditions in JavaScript

Is there anyways to pass the condition for if statement dynamically along with its operator without using eval() or new function() that affects it's performance.

<script>

var Students =  [{
            "name": "Raj",
             "Age":"15",
            "RollNumber": "123",
            "Marks": "99",
              
        }, {
            "name": "Aman",
             "Age":"14",
            "RollNumber": "223",
            "Marks": "69",
           },
           {
            "name": "Vivek",
             "Age":"13",
            "RollNumber": "253",
            "Marks": "89",
           }
        ];



*Number of condition are dynamic. can even contain 3 or 4 condition at a time*

var condition = [{"attribute":"el.Age","operator":">=","value":14},
                 {"attribute":"el.Marks","operator":"<=","value":70}];

var newArray =Students.filter(function (el)
{
  if( condition[0].attribute condition[0].operator condition[0].value && condition[1].attribute condition[1].operator condition[1].value ){
  
return true;
  
});

console.log(newArray);
</script>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Yes, you pass the operands and the operator into a function that dispatches based on the operator to something that does the operation.

As a code sketch:

const operators = new Map([
    ["<", lt],
    [">", gt],
    ["<=", lte],
    [">=", gte],
    // ...
]);
function evaluate(operator, leftOperand, rightOperand) {
    const evaluator = operators.get(operator);
    if (!evaluator) {
        throw new Error(`Unknown operator ${operator}`);
    }
    return evaluator(leftOperand, rightOperand);
}

...where lt, gt, etc. are functions that perform the operation. I've used operator, leftOperand, and rightOperand above, but you could use attribute and value if you prefer. You'll need something like this to get the value of the "attribute" (the JavaScript term is "property").

(Or you could do the operations in the evaluate function itself in a big switch.)

Then you use that function in the filter:

let newArray = Students.filter(student => conditions.every(({operator, attribute, value}) => {
    const leftOperand = getProperty(student, attribute);
    return evaluate(operator, leftOperand, value);
}));

I've used every there because you're using an && condition. (Note I renamed the condition array to conditions since arrays hold more than one value.)

about 4 years ago · Juan Pablo Isaza 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