3.0.1 • Published 10 years ago

foreach-extended v3.0.1

Weekly downloads
2
License
-
Repository
github
Last release
10 years ago

foreach + break and continue

Iterate over the key value pairs of either an array-like object or a dictionary like object + break and continue constructs.

browser support

API

foreach(object, function, context)

var each = require('foreach');

var result = each([1,2,3], function (value, key, array) {
    console.log([key, value]);  // Prints out:  [0, 1] then [1, 2] then iteration stops

    if (key == 1) return [key, value, array]; // break; 
});

console.log(result); // Prints out [1,2, [1, 2, 3]]

each({0:1,1:2,2:3}, function (value, key, object) {
    // Simple object iteration
});


// When it is required also to break the outer loop
var result = each([[1, 2, 3], [3, 4, 5], {"5": "6", "7": "8"}, [1,2,3,4,5]], function (value, key, array) {
    return each(value, function (value, key, object) {
        return value === "6" ? "This value is returned by foreach" : undefined; // returning undefined means that iteration should continue (it is also an equivalent of `continue` call)
    });

    if (key == 3) return [key, value, array]; // break; (this break will not execute)
});

console.log(result); // Prints out "This value is returned by foreach"
3.0.1

10 years ago

3.0.0

10 years ago