Created
April 27, 2012 16:43
-
-
Save mkuklis/2510679 to your computer and use it in GitHub Desktop.
underscore _.filterAll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (_, slice) { | |
_.filterAll = function (data) { | |
var filters = slice.call(arguments, 1); | |
return _.filter(data, function (item) { | |
var found = true; | |
for (var i = 0, l = filters.length; i < l; i++) { | |
found &= filters[i](item); | |
} | |
return found; | |
}); | |
} | |
})(_, Array.prototype.slice); | |
var f1 = function (item) { | |
return item.a == 1; | |
} | |
var f2 = function (item) { | |
return item.b == 2; | |
} | |
var data = [{a: 1, b: 2, c: 3}, {a: 1, b: 2, c: 3}, {a: 3, b: 2, c: 1}]; | |
_.filterAll(data, f1, f2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment