Skip to content

Instantly share code, notes, and snippets.

@Rickgg
Created December 6, 2017 19:00
Show Gist options
  • Select an option

  • Save Rickgg/71a9045fdbb4e7c7a11df30162e67a11 to your computer and use it in GitHub Desktop.

Select an option

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.
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