Last active
September 10, 2015 11:49
-
-
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).
This file contains 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 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; | |
} |
This file contains 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 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