Last active
April 19, 2018 14:57
-
-
Save mota57/0b7b34a1a75dda1a6956b5adca2ed6d8 to your computer and use it in GitHub Desktop.
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 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":[{}] | |
}] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I hope somebody could find this util for them.