在jQuery中过滤具有多个记录ID的分层JSON数据

时间:2012-06-22 16:32:57

标签: jquery regex json filter match

参考以下链接:

https://stackoverflow.com/posts/4391119/revisions

Filter JSON Data with multiple record IDs in jQuery

如果JSON中有分层数据怎么办?我已尝试在该filterStore函数中进行修改但未成功。你能帮帮我吗?

当前的filterStore是这样的:

var filter = {
    "brand_id": [1,2,3],
    "productname": new RegExp('(.*?)', 'gi'),
    "price": new RegExp('.*?', 'gi')
};

function filterStore(dataStore, filter) {
    return $(dataStore).filter(function(index, item) {
        for( var i in filter ) {
            if(filter[i] instanceof Array){   
              if($.inArray(parseInt(item[i],10),filter[i]) == -1)
                 return null;
              else
                 continue;                  
            }
           if( ! item[i].toString().match( filter[i] ) ) return null;
        }
        return item;
    });
}

但是json的反应是这样的:

[
    {
      "brandInfo": {
          "brand": "Lg",
          "productname": "Microwave",
      },
      "prodInfo": {
          "size": "1.5 ltr",
          "price": 200,
          "color": "black"
      },
      "Category": "Electronic",
      "shop": "Walmart"
    }
    {
      "brandInfo": {
          "brand": "Samsung",
          "productname": "Microwave",
      },
      "prodInfo": {
          "size": "1.5 ltr",
          "price": 250,
          "color": "Ivory"
      },
      "Category": "Electronic",
      "shop": "Walmart"
    }
    {
      "brandInfo": {
          "brand": "Toshiba",
          "productname": "Microwave",
      },
      "prodInfo": {
          "size": "1.6 ltr",
          "price": 310,
          "color": "Silver"
      },
      "Category": "Electronic",
      "shop": "Walmart"
    }
    {
      "brandInfo": {
          "brand": "Hitachi",
          "productname": "Microwave",
      },
      "prodInfo": {
          "size": "1.5 ltr",
          "price": 280,
          "color": "black"
      },
      "Category": "Electronic",
      "shop": "Walmart"
    }
]

Joy,你能帮我设置像这样的分层数据的过滤器吗?新功能filterStore?

1 个答案:

答案 0 :(得分:0)

首先重写过滤器以反映嵌套结构。

var filter = {
    "brandInfo": {
        "brand_id": [1,2,3]
    },
    "prodInfo": {
        "productname": new RegExp('(.*?)', 'gi'),
        "price": new RegExp('.*?', 'gi')
    }
};

现在您可以像这样编写过滤器代码(只需添加一个嵌套循环来循环遍历外部属性,如 brandInfo )。

function filterStore(dataStore, filter) {
    return $(dataStore).filter(function(index, item) {
        for( var prop in filter ) {
            if (item[prop] == null) return null;
            for( var i in prop ) {
                if (item[prop][i] == null) return null;
                if(filter[prop][i] instanceof Array){  
                    if($.inArray(parseInt(item[prop][i],10),filter[prop][i]) == -1) return null;
                    else continue; 
                }
                if( ! item[prop][i].toString().match( filter[prop][i] ) ) return null;
            }
        }
        return item;
    });
}

这应该适用于您的示例中的数据结构。但请记住,此代码假定您的过滤器仅包含二级嵌套属性。对于过滤嵌套属性的更通用的解决方案,您需要将过滤器逻辑放在一个单独的函数中并以递归方式使用它。