递归搜索的全局变量和它的属性值 [英] Recursively search for a value in global variables and its properties

查看:95
本文介绍了递归搜索的全局变量和它的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我想搜索的值,如计算器,在所有声明的变量在窗口
我可以用这个code做到这一点:

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 p in obj)
        if(obj[p] == value)
            return(p);
}
globalSearch(window, 'StackOverflow');

这code将返回这个值(或没有返回值)变量的名字。
所以,如果我宣布以价值计算器变量,它会成功地找到它。

This code will return the name of a variable that have this value (or returns nothing). So, if I have declared a variable with value 'StackOverflow', it will successfully find it.

我的问题是,我想更深入和搜索直通窗口的对象(和它自己的嵌套对象)也一样,要实现这样的结果:

My problem is that I want to go deeper and search thru window's objects (and its own nested objects) too, to achieve a result like this:

var x = 'StackOverflow'                     // returns 'x'
var y = { a : 'StackOverflow' }             // returns 'y.a'
var z = { a : { b: 'StackOverflow' } }      // returns 'z.a.b'

我在与对象的继承方法的问题。有没有办法做到这一点?

I'm having problems with inherited methods of Objects. Is there a way to do this?

推荐答案

深的搜索,但没有递归函数调用

功能递归有内部堆栈限制和浪费内存。

Functional recursion has internal stack limits and wastes memory.

附加功能的加入

在一个搜索阵列的形式递归对象保护;它不使用起来当然太多内存的对象作为引用只存储。

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.

如果该对象本身的值相匹配,则返回true。否则,它会返回''这将匹配为假。

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

阵列使用角括号标记。

的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;
}

问题

不传递intial变量名到函数,我们不能从一开始就返回完全合格的变量名。我想不出一个解决方案,我woud感到惊讶,如果有一个。

Without passing the intial 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 woud be surprised if there was one.

带有空格的变量名称是为关键,以一个对象有效,如其他非法的变量名,它只是意味着该值必须用尖括号加以解决。有一对夫妇的解决方案,我能想到的。正则表达式检查每个变量名,以确保它是有效的,并使用角括号标记,如果事实并非如此。这种压倒一切的问题在于,REG-EX是一个页长。另外,我们只能用尖括号但这不是真的到OP的原题。

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.

阵列上的电话的indexOf'搜索'可能是非常大的物体有点沉重,但我还不能想到一个替代的。

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.

改进

除了清理code一点,它也将是很好,如果函数返回匹配的数组。这也引起在返回数组中不含有递归对象的引用的另一个问题。也许功能可以接受的结果格式配置参数。

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.

这篇关于递归搜索的全局变量和它的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆