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

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

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

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

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 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