2

Couldn't really find an answer for this. I have a function which should allow users to pass it "checks" (functions that returns true or false). The checks will run on a large number of items. For each item I want to know if all the checks returned true.

function foo(checksArray) { //checksArray: [func1, func2, func3]
  var itemList = [1, 2, 3, 4];
  for (item of itemList)
    if (checkAllFunctions(item))
      doSomething();
}

How can I do it? Obviously I can iterate over each function with a for loop but I suspect there might be a better way. Maybe there's even a one-liner.

Thanks for any help guys.

Edit: I guess there isn't really any point in keeping running even though one of checks returned false. If it can stop right there, that's even better!

5
  • 2
    Should all the functions get called even if one of them returns false, or should foo bomb out as soon as possible? Commented Nov 16, 2015 at 11:16
  • is the doSomething method sync or async? Commented Nov 16, 2015 at 11:17
  • 2
    Array.prototype.every or Array.prototype.some Commented Nov 16, 2015 at 11:18
  • @JamesThorpe Edited question, I should have mentioned it beforehand. @AdityaParab Should be sync. @Andreas I'll check them, thanks! Commented Nov 16, 2015 at 11:18
  • 1
    The simplest approach is to use Array.prototype.every. Something like this itemList.every( item => checksArray.every(check => check(item))) Commented Nov 16, 2015 at 11:18

3 Answers 3

3

Use forEach to take up each element of itemList. Inside that loop, use every to check if every function in checksArray passes.

function foo(checksArray) { //checksArray: [func1, func2, func3]
  var itemList = [1, 2, 3, 4];

  itemList.forEach(function(item) {
    if (checksArray.every(function(check) { return check(item); })) doSomething();
  });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly! Close to @YuryTarabanko's comment. Thanks!
1

You can use simple flag for that. Try something like this:

function foo(checksArray) { //checksArray: [func1, func2, func3]
    var itemList = [1, 2, 3, 4];

    var flag = true;

    for (item of itemList)
        flag = checkElement(item); // check single element, not all

        if(flag){
            // here you can check your state after every single item
        }
    }

    if(flag){
        doSomething();
    }
}

1 Comment

Could you be missing an iterator? I don't see a reference to checksArray. Also, I mentioned I know I can do this with a loop but wanted to know if there's a shorter/cleaner/faster way.
1

Not necessarily shorter, but cleaner and correct:

function foo (checksArray) {
    var itemList = [1, 2, 3, 4];
    var meetsAllCriteria = itemList.every(function (item) {
        return checkAllFunctions(item);
    });
    if (meetsAllCriteria) {
        doSomething();
    }
}

3 Comments

I actually want the doSomething() to run every time the checks for an item return true. checkAllFunctions(item) is what I'm interested in.
Are you using checksArray?
Also, how is function(item) { return checkAllFunctions(item); } different from just checkAllFunctions?

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.