Created
February 16, 2019 22:58
-
-
Save jroakes/cdeb9d6afb9559573111e74ffb315c4b 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
var items = [] | |
console.log('\n\n Starting:') | |
function globalSearch(obj, value, maxrec, rec = 0 ) { | |
var excl = ['document','window','top'] | |
if (rec <= maxrec ){ | |
for(var p in obj){ | |
var result = false | |
if (excl.indexOf(p) == -1 && obj[p] ){ | |
if( p.toLowerCase().indexOf(value) > -1 ){ | |
result = p; | |
}else if( typeof obj[p] === "string" && obj[p].toLowerCase().indexOf(value) > -1 ){ | |
result = p; | |
}else if(typeof obj[p] === "object" && obj[p] != obj){ | |
var recl = rec+1 | |
var te = globalSearch(obj[p], value, maxrec, recl); | |
if (te) | |
result = p + "." + te | |
} | |
} | |
if (result && rec == 0){ | |
items.push(result) | |
}else if (result && rec > 0){ | |
return result; | |
} | |
} | |
} | |
return false; | |
} | |
globalSearch(window, "<string to search>", 7) |
dwsmart
commented
Feb 18, 2019
Window Search Version:
function globalSearch(obj, search, maxrec, rec = 0 ) {
var result = [];
var excl = ['document','window','top']
if (rec <= maxrec ){
Object.keys(obj).forEach( function(key) {
if(excl.indexOf(key) == -1 ) {
var type = typeof obj[key];
if(type === "string") {
if (obj[key]) {
if( obj[key].toLowerCase().indexOf(search) > -1 ){
result.push({key: key, value: obj[key]});
}
}
}
if(type === "object" && obj[key] != obj) {
if (obj[key]) {
var recl = rec+1
var te = globalSearch(obj[key], search, maxrec, recl)
if (te) {
te.forEach(function(rs) {
result.push({key: key + '.' + rs.key, value: rs.value});
})
}
}
}
}
});
return result
}
}
console.log(globalSearch(window, 'SearchString', 50));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment