如何检查JSON数组包含不同/多个键 - 值对

时间:2013-02-01 19:26:54

标签: javascript jquery json

我有一个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>

目前我已经制作了一个运行良好的解决方案,仅使用一个键和值进行过滤,这不是重点。问题是获取对象:对象:{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的解决方案和我当前的doenst工作以及我需要它。我也无法找到JS解决它的正确方法。我的问题不重复,因为发布的链接处理单一方式,而不是多方式。

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

1 个答案:

答案 0 :(得分:0)

  

我的问题是如何获取所有类型的条目:0并将此信息保存到Javascript数组 - JohnDoo 1分钟前

这很容易。

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]);
}