2

With Reference of below links:

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

Filter JSON Data with multiple record IDs in jQuery

What if there is a Hierarchical Data in JSON.. I've tried making modification in that filterStore function.. but not being succeeded. Can you help me with that??

Current filterStore is like this:

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

but json response is something like this:

[
    {
      "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, Can you help me set filters for hierarchical data like this?? new function filterStore?

1
  • use jLinq .. makes working with data object a dream Commented Jun 22, 2012 at 17:26

1 Answer 1

0

Start by re-writing the filter to reflect the nested structure.

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

Now you can write the filter code like this (just adding a nested loop to cycle through the outer properties like 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;
    });
}

This should work as far as the data structure in your example goes. But keep in mind, this code assumes that your filter contains only second-level nested properties. For a more general solution to filtering nested properties, you'd need to put your filter logic in a separate function and use it recursively.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.