Skip to content

Instantly share code, notes, and snippets.

@GideonPARANOID
Last active September 10, 2015 11:49
Show Gist options
  • Save GideonPARANOID/d01106e0d5c86178f4b4 to your computer and use it in GitHub Desktop.
Save GideonPARANOID/d01106e0d5c86178f4b4 to your computer and use it in GitHub Desktop.
Finds a match for an attribute/value pair in an object tree (uses lodash).
function find(subtree, attribute, value) {
var result = null;
subtree.hasOwnProperty(attribute) && subtree[attribute] === value ?
result = subtree :
_.forEach(Object.keys(subtree), function (key) {
if (typeof subtree[key] === 'object') {
var next = find(subtree[key], attribute, value);
if (next != null) {
result = {};
result[key] = next;
return false;
}
}
});
return result;
}
function find(subtree, attribute, value) {
var result = null;
subtree.hasOwnProperty(attribute) && subtree[attribute] === value ?
result = subtree :
_.forEach(Object.keys(subtree), function (key) {
if (typeof subtree[key] === 'object') {
var next = find(subtree[key], attribute, value);
if (next != null) {
result = next;
return false;
}
}
});
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment