Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get arbitrarily deeply nested objects with a comparator.
/**
* Get deeply nested objects using a 'compare' function.
*
* @param {(object|array)} target Data tree in which to find objects.
* @param {function} compare Passed items in the `target` data tree. It
* should return a boolean value based on the
* item.
* @return {array}
*/
function getDeepObjects(target, compare) {
var found = [];
if (Array.isArray(target)) {
target.forEach(function(childTarget) {
[].push.apply(found, findDeep(childTarget, compare));
});
} else if (target instanceof Object) {
if (compare(target)) {
found.push(target);
} else {
Object.keys(target).forEach(function(key) {
[].push.apply(found, findDeep(target[key], compare));
});
}
}
return found;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.