Skip to content

Instantly share code, notes, and snippets.

@greaveselliott
Created February 28, 2018 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greaveselliott/8450eba7cb2313b0d4b143f747751b8c to your computer and use it in GitHub Desktop.
Save greaveselliott/8450eba7cb2313b0d4b143f747751b8c to your computer and use it in GitHub Desktop.
Searches JS object for values
function find_property(object, property) {
var results = [];
for (var key in object) {
if (object !== undefined) {
var value = object[key];
if (key === property) {
results.push({
"property": key,
"value": value
});
}
if (typeof value === 'object') {
var inner_result = find_property(value, property);
if (inner_result.length > 0 && inner_result !== undefined) {
results.push(inner_result[0]);
}
}
}
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment