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

116
Vistas
Using Array.prototype.every or Array.property.some depending on condition

I'm trying to make a function to filter some data using OR/AND logical operator depending on what the user selects. That means I need to use Array.prototype.every (AND) or Array.property,some (OR) to fullfill my requirement.

To avoid code redundancy I was trying to do something like this:

const functionApplied = condition === 'ALL'
        ? myArray.every
        : myArray.some;

functionApplied(item =>  {
    ... aux code ...
})
functionApplied.call(item =>  {
    ... aux code ...
})

Is there an analogous way to do it like this?

I know I could do something like this, but I'm just curious about above sintax...

const auxFunction = (item) => {...};
if (condition === 'ALL') {
    myArray.every(item => auxFunction(item));
} else {
    myArray.some(item => auxFunction(item));
}

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

0

You can definitely do something similar to what you're hoping to achieve. Here's one way:

function checkCondition(condition, booleanFunc, myArray) {
    const functionApplied = condition === 'ALL'
        ? myArray.every.bind(myArray)
        : myArray.some.bind(myArray);

    return functionApplied(booleanFunc);
}

console.log(checkCondition('ANY', (item) => {return item > 2}, [1,4,5,6]));
console.log(checkCondition('ANY', (item) => {return item > 2}, [4,5,6]));
console.log(checkCondition('ANY', (item) => {return item > 2}, [1,2]));
console.log(checkCondition('ALL', (item) => {return item > 2}, [1,4,5,6]));
console.log(checkCondition('ALL', (item) => {return item > 2}, [4,5,6]));

VLAZ comment is another good direction.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use a variable holding the property name, and then use dynamic property access.

function functionApplied(condition) {
  return functionApplied === 'ALL' ? 'every' : 'some';
}

let result = myArray[functionApplied(condition)](item => { ... });

about 4 years ago · Juan Pablo Isaza Denunciar

0

Yep, VLAZ comment is the way I wanted, this works:

const functionApplied = condition === 'ALL'
        ? Array.prototype.every
        : Array.prototype.some;

functionApplied.call(myArray, item =>  {
    ... aux code 

Thanks everyone for your quick answers :)

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