Skip to content

Instantly share code, notes, and snippets.

@depoulo
Last active August 29, 2015 14:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save depoulo/bd6e33daf480d07172a6 to your computer and use it in GitHub Desktop.
Save depoulo/bd6e33daf480d07172a6 to your computer and use it in GitHub Desktop.
_.whereDeep
/**
* 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