Skip to content

Instantly share code, notes, and snippets.

@mota57
Last active April 19, 2018 14:57
Show Gist options
  • Save mota57/0b7b34a1a75dda1a6956b5adca2ed6d8 to your computer and use it in GitHub Desktop.
Save mota57/0b7b34a1a75dda1a6956b5adca2ed6d8 to your computer and use it in GitHub Desktop.
function findTByKeyValue (element, target){
var found = true;
for(var key in target) {
if (!element.hasOwnProperty(key) || element[key] !== target[key]) {
found = false;
break;
}
}
if(found) {
return element;
}
if(typeof(element) !== "object") {
return false;
}
for(var index in element) {
var result = findTByKeyValue(element[index],target);
if(result) {
return result;
}
}
};
/*
example
findTByKeyValue(problems,{"name":"somethingElse","strength":"500 mg"}) =====> result equal to object associatedDrug#2
var problems = [{
"Diabetes":[{
"medications":[{
"medicationsClasses":[{
"className":[{
"associatedDrug":[{
"name":"asprin",
"dose":"",
"strength":"500 mg"
}],
"associatedDrug#2":[{
"name":"somethingElse",
"dose":"",
"strength":"500 mg"
}]
}],
"className2":[{
"associatedDrug":[{
"name":"asprin",
"dose":"",
"strength":"500 mg"
}],
"associatedDrug#2":[{
"name":"somethingElse",
"dose":"",
"strength":"500 mg"
}]
}]
}]
}],
"labs":[{
"missing_field": "missing_value"
}]
}],
"Asthma":[{}]
}]
*/
@mota57
Copy link
Author

mota57 commented Apr 20, 2017

I hope somebody could find this util for them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment