0

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));
}

2
  • "I was trying to do something like this": please explain which problem you encountered with that approach. Commented May 12, 2022 at 16:25
  • 1
    The proper .call usage is functionApplied.call(myArray, item => { ... aux code ... }) since you need to supply the value for this. Commented May 12, 2022 at 16:26

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

I would personally use the following:

const fn = condition === 'ALL' ? "every" : "some";
const result = myArray[fn]((item) => {
  // ... aux code ...
});

Storing the function name every or some in a variable called fn. Then calling it supplying your aux code.

Comments

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 => { ... });

Comments

0

const result = (myArray[condition === 'ALL' ? 'every': 'some'])(item => auxFunction(item))

this is minimal one line answer you can find

feel free to to seprate condition and min looping function

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.