Last active
August 29, 2015 14:27
-
-
Save depoulo/bd6e33daf480d07172a6 to your computer and use it in GitHub Desktop.
_.whereDeep
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
/** | |
* Pimped version of `_.where` (https://lodash.com/docs#where) that can recurse deep. | |
* | |
* @param {Array|Object|string} collection The collection to search. | |
* @param {Object} source The object of property values to match. | |
* @param {String} prop The property of the collection to recurse into. | |
* @returns {Array} the new filtered array. | |
*/ | |
var whereDeep = function (collection, source, prop) { | |
'use strict'; | |
return _.where(collection, source).concat(_.flatten(_.map(collection, function (item) { | |
return whereDeep(item[prop], source, prop); | |
}))); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment