JSFiddle - React, Tailwind, and code Playground
Recursively Search for Variables by Type
by skibulk
JavaScript
// Do not search window on JSFiddle - the frame window links to the parent window, and will cause a cross origin access violation
function Gmap(){}
gmap = new Gmap;
test = {test: {test: {test: gmap}}};
console.log(search_by_type(test, "Gmap", 5));
function search_by_type(scope, type, depth) {
var set = [], val, subset;
set.scope = scope;
if (depth > 0) {
depth--;
for (var key in scope) {
val = scope[key];
if (val && val.constructor && val.constructor.name == type) {
// Found search result
set.push([key, val]);
} else if (val !== null && typeof val === 'object') {
// Recurse children
subset = search_by_type(val, type, depth);
if (subset.length) {
// The Children contain results
set.push([key, subset]);
}
}
}
}
return set;
}