36 lines
873 B
JavaScript

let dig = (value, startObject=window) => {
var stack = [[startObject, '']];
var searched = [];
var found = false;
var isArray = function (test) {
return Object.prototype.toString.call(test) === '[object Array]';
}
while (stack.length) {
var fromStack = stack.pop();
var obj = fromStack[0];
var address = fromStack[1];
if (typeof obj == typeof value && obj == value || typeof obj === 'string' && value instanceof RegExp && value.test(obj)) {
var found = address;
break;
} else if (typeof obj == "object" && searched.indexOf(obj) == -1) {
if (isArray(obj)) {
var prefix = '[';
var postfix = ']';
} else {
var prefix = '.';
var postfix = '';
}
for (i in obj) {
try {
stack.push([obj[i], address + prefix + i + postfix]);
}
catch {}
}
searched.push(obj);
}
}
return found == '' ? true : found;
}