0

我有一个 JSON 数组,想实现一个过滤引擎,我尝试找到一个解决方案,用一个特殊的键:值对获取所有这些条目并将其推送到一个 sep 数组中。

JSON 看起来像这样:

var sample = {
    "response": {
        "things": [{
            "index": 0,
            "type": 1,
            "img":2
        },{
            "index": 1,
            "type": 0,
            "img":1
        },{
            "index": 2,
            "type": 1,
            "img":1
        }]
    }
};

示例 HTML:

<select name="type">
<option value="0">one</option>
<option value="1">two</option>
...
</select>

目前我已经制定了一个运行良好的解决方案,仅使用一个键和值进行过滤,这不是重点。问题是获取对象: Object:{param:1,param:2,param:3,param:n} 并找到具有这些过滤器对的所有条目。

我没有找到正确的方法来处理多个或如何在这里使用,所以我希望有人可以帮忙。

        var uri =['http://example.com?type=1&img=0'];

        for (var i = 0, len = uri.length; i < len; i++) {
            var result = this.qwertzSplit(uri[i]);
        }
        testing(result);
//qwertzSplit contains the Object: Object{type="1",img="0"}
    function testing(result){
        var obj = this.sample,indexes = [];

        for (result in obj) {
            if (obj.hasOwnProperty(result)) {
                indexes.push(result);
            }
        }
       console.log(indexes);
}

因此,就我而言,kolink 的解决方案和我当前的解决方案可以满足我的需要。而且我也找不到正确的 JS 方法来解决它。我在这里的问题不是重复的,因为发布的链接处理单一方式,而不是多种方式。

所以我必须找到一种方法来获取具有给定键/值对的所有条目,这些键/值对取出 URI

4

1 回答 1

0

my question is how get all entries with the type:0 and save this information into a Javascript array – JohnDoo 1 min ago

That's easy.

var out = [], i, l = sample.response.things.length;
for( i=0; i<l; i++) {
    if( sample.response.things[i].type == 0) out.push(sample.response.things[i]);
}
于 2013-02-01T19:38:51.247 回答