Created
December 6, 2017 19:00
-
-
Save Rickgg/71a9045fdbb4e7c7a11df30162e67a11 to your computer and use it in GitHub Desktop.
Function to recursively search an object by a key and returning the data found in the key.
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 iterate (obj, key) { | |
| var result; | |
| for (var property in obj) { | |
| if (obj.hasOwnProperty(property)) { | |
| // in case it is an object | |
| if (property.match(RegExp(key, 'gi'))) { | |
| return obj[property]; // returns the value | |
| } else if (typeof obj[property] === "object") { | |
| result = iterate(obj[property], key); | |
| if (typeof result !== "undefined") { | |
| return result; | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment