0

我必须再次用更多细节重新发布这个问题:

我有一个 JSON 树array

JSON 树的结构如下所示:

{
"app": {

    "categories": {

        "cat_222": {
            "id": "555",
            "deals": [{
                "id": "73",
                "retailer": "JeansWest"

            }, {
                "id": "8630",
                "retailer": "Adidas"

            }, {
                "id": "11912",
                "retailer": "Adidas"

            }]
        },

        "cat_342": {
            "id": "232",
            "deals": [{
                "id": "5698",
                "retailer": "KFC"
            }, {
                "id": "5701",
                "retailer": "KFC"
            }, {
                "id": "5699",
                "retailer": "MC"

            }]
        }
    }
  }
}

现在,我想过滤这个 JSON 树var pattern="KF"

返回所有包含retailer名称KF的内容及其 id。

=======================更新===========================

只需检查我的另一个问题。它得到了解决。 过滤多维 JSON 数组

4

2 回答 2

1

使用Array.filter或者_.filter如果你需要支持 IE < 9

于 2013-08-29T07:07:04.180 回答
1

Well, you can use _.filter:

var filteredArray = _.filter(arrayOfStrings, function(str) { 
  return str.indexOf(data) !== -1; });

... or jQuery.grep:

var filteredArray = $.grep(arrayOfStrings, function(str) { 
  return str.indexOf(data) !== -1; });

As you see, the approaches are quite similar - and, in fact, both use Array.filter, if it's available in the host environment.

Also note that the original array is not affected here. If you want otherwise, just assign the result of filtering to the same variable (i.e., arrayOfStrings in this example).

于 2013-08-29T07:05:42.613 回答