在javascript对象中搜索具有特定值的属性? [英] Search a javascript object for a property with a specific value?

查看:141
本文介绍了在javascript对象中搜索具有特定值的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个javascript对象,我想递归搜索它以查找包含特定值的任何属性。

I have a javascript object, and I want to recursively search it to find any properties that contain a specific value.

javascript我的工作已经缩小,并不容易追踪。

The javascript I'm working with has been minified, and is not so easy to trace through.

背景

我正在使用Bing Maps AJAX SDK。它可以添加其他图块层。每个tilelayer都有一个tilesource对象,它指定了tile URL的URI格式。

I'm using the Bing Maps AJAX SDK. It has the ability to add additional tile layers. Each tilelayer has a tilesource object, which specifies the URI format for the tile URL.

我遇到了一个问题,即tilesource URI创建一次并缓存。因此,我不能为每个请求动态更改URL的参数(例如,根据一天中的时间更改图块叠加层的颜色)。

I've ran into a problem where the tilesource URI is created once, and cached. Thus I can't dynamically change the parameters of the URL (for example, to change the colors of the tile overlay based on the time of day) for each request.

请注意,此行为与Google的Map API和WP7的Bing Maps api不同,后者允许您为每个磁贴请求动态创建URL。

Note that this behavior is different that Google's Map API, and the Bing Maps api for WP7, which both allow you to dynamically create the URL for each tile request.

缓存查找URI,替换两个特定参数,然后使用URI获取磁贴。

The cached URI is looked up, and two specific parameters are replaced, then the URI is used to fetch the tile.

由于这是javascript,我想找到缓存的URI,并用一个函数替换它,而不是动态构建URI,然后返回它。

Since this is javascript, I'd like to find the cached URI, and replace it with a function, that instead dynamically builds the URI, and returns it.

我不需要在每个运行时都这样做,只是想知道缓存属性的位置,所以我可以将代码编写到hax0r中。

I don't need to do this each runtime, just want and idea of where the property is being cached, so I can write code to hax0r it.

原始问题

如果我将URI设置为某个值,例如floobieblaster,那么当我设置一个断点,我可以递归搜索javascript对象的floobieblaster并获取存储该值的属性吗?

If I set the URI to some value like "floobieblaster", when I set a breakpoint, can I search the javascript object recursively for "floobieblaster" and get the property that is storing that value?

编辑添加

我正在搜索的对象似乎有一个循环引用,因此任何递归代码都可能导致堆栈溢出。

The object I'm searching appears to have a circular reference, thus any recursive code will likely cause a stackoverflow.

我可以使用任何编辑器/调试器技巧吗?

Are there any editor/debugger tricks I could make use of?

推荐答案

像这样的简单应该可以工作:

Something simple like this should work:

var testObj = {
    test: 'testValue',
    test1: 'testValue1',
    test2: {
        test2a: 'testValue',
        test2b: 'testValue1'
    }
}

function searchObj (obj, query) {

    for (var key in obj) {
        var value = obj[key];

        if (typeof value === 'object') {
            searchObj(value, query);
        }

        if (value === query) {
            console.log('property=' + key + ' value=' + value);
        }

    }

}

如果执行 searchObj(testObj,'testValue'); ,它会将以下内容记录到控制台:

If you execute searchObj(testObj, 'testValue'); it will log the following to the console:

property=test value=testValue
property=test2a value=testValue

显然,你可以用你想要的任何东西替换 console.log ,或者在 searchObj 函数使其更具可重用性。

Obviously, you can replace the console.log with whatever you want, or add a callback parameter to the searchObj function to make it more reusable.

编辑:添加查询参数,允许您在调用函数时指定要搜索的值。

Added the query parameter which allows you to specify the value you want to search for when you call the function.

这篇关于在javascript对象中搜索具有特定值的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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