Last active
February 26, 2018 07:17
-
-
Save jalalazimi/c65fcee20991c1cf8ccd08736d449ac5 to your computer and use it in GitHub Desktop.
Find property by name in a deep object
This file contains hidden or 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 findPropertyInDeepObj(obj, key) { | |
if (_.has(obj, key)) | |
return [obj]; | |
var res = []; | |
_.forEach(obj, function(v) { | |
if (typeof v == "object" && (v = findPropertyInDeepObj(v, key)).length) | |
res.push.apply(res, v); | |
}); | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment