Skip to content Skip to sidebar Skip to footer

Recursively Search For A Value In Global Variables And Its Properties

Let's say that I want to search for a value, like 'StackOverflow', in all declared variables in window. I can do it with this code: function globalSearch(obj, value) { for(var

Solution 1:

Deep search but without the recursive function calls

Functional recursion has internal stack limits and wastes memory.

Additional features added

Recursive object protection in the form of a searched array; It doesn't use up too much memory of course as the objects are only stored as references.

Return true if the the object itself matches the value. Otherwise it would return '' which would match to false.

Arrays use angle-bracket notation.

The code

function globalSearch(startObject, value) {
    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) {
            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 ) {
              stack.push( [ obj[i], address + prefix + i + postfix ] );
           }
           searched.push(obj);
        }
    }
    return found == '' ? true : found;
}

Problems

Without passing the initial variable name into the function, we can't return the fully qualified variable name from the beginning. I can't think of a solution and I would be surprised if there was one.

Variable names with spaces are valid as the key to an object, as are other invalid variable names, it just means that the value must be addressed using angle-brackets. There are a couple of solutions I can think of. Regex check each variable name to make sure it's valid and use angle-brackets notation if it is not. The overriding problem with this is that the reg-ex is a page long. Alternatively, we could only use angle-brackets but this isn't really true to the OPs original question.

The indexOf call on the array 'searched' might be a bit heavy on very large objects but I can't yet think of an alternative.

Improvements

Apart from cleaning up the code a little, it would also be nice if the function returned an array of matches. This also raises another issue in that the returned array would not contain references to recursive objects. Maybe the function could accept a result format configuration parameter.


Solution 2:

This should work. It uses recursion to achieve the result.

function globalSearch(obj, value) {
    for(var p in obj)
        if(obj[p] == value){
            return(p);
        }else if(typeof obj[p] == "object" && obj[p] != obj){
           var te = globalSearch(obj[p], value);
           if(te!=false){ return p + "." + te }
        }
    return false;
}

Solution 3:

Make your solution recursive. If you have an object, call your function again.

function globalSearch(obj, value) {
    for(var p in obj) {
        if (obj[p] == value) {
            return(p);
        } else if (typeof obj[p] === "object") {
            var recursiveCheck= globalSearch(obj[p], value);
            if (recursiveCheck) {
                return p + "." + recursiveCheck;
            }
        }
    }
}
globalSearch(window, 'StackOverflow');

I bet most browsers will hit a warning for too much looping.


Solution 4:

This code, based on the other answer, allows for all possible value matches to be found.

function globalSearch(startObject, value, returnFirstResult = false) {
    var stack = [[startObject,'']];
    var searched = [];
    var found = new Set();

    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) {
            if (returnFirstResult) {
                return address == '' ? false : address;
            }
            found.add(address)
        }if(typeof obj == "object" && searched.indexOf(obj) == -1){
           if ( isArray(obj) ) {
                var prefix = '[';
                var postfix = ']';
           }else {
                var prefix = '.';
                var postfix = '';
           }
           for( i in obj ) {
                stack.push( [ obj[i], address + prefix + i + postfix ] );
           }
           searched.push(obj);
        }
    }
    return Array.from(found);
}

Post a Comment for "Recursively Search For A Value In Global Variables And Its Properties"