How can I break or continue knockout's arrayforeach?

Viewed 14504

This is sample code. I want to break (or continue) knockout's ko.util.arrayForEach.

ko.utils.arrayForEach(items, function (item) {
    if (...) break;
    if (...) continue;
}
3 Answers

Just use ko.utils.arrayFirst that will execute a function on the array and and will return/break as soon as the the function evaluates to true.

Here is realife example that I recently used

this.updateElement = function (model) {
    ko.utils.arrayFirst(that.list(), function (m) {
        if (model.formId() === m.formId()) {
            m.description(model.description());
            m.status(model.status());
            m.type(model.type());
        }
    });
}

More details can be found here http://www.knockmeout.net/2011/04/utility-functions-in-knockoutjs.html

Related