Tuesday, January 28, 2014

Working with filtering JSON objects

Following are some way by which we can filter out objects in JSON format using jQuery.

(1) filter : using this method we can filter out JSON data by following way..
var filterData = (JSONDATA.filter(function (item) {
            return item["ColumnName"] != "Your Data To Compare";
        }));
- In above situation, filter will loop through each data and finds the specific data by comparing it.
- filter is only available in new version of jQuery. Also, it is not working in IE-8 and not compatible with IE-8 or lower.

(2) grep : using this method we can filter out JSON data by following way..
var filterData = ($.grep(JSONDATA,function (item,l) {
            return item["ColumnName"] != "Your Data To Compare";
        }));
In above situation, grep will loop through each data and finds the specific data by comparing it.


(3) slice : using this method we can removes and returns a given number of elements starting from a given index from JSON object by following way..
var filterData = $.each(JSONDATA.slice(1,3), function(item) {
                    return item;

                }),
- In above example, removes three elements starting from index position 1 (i.e., the 2nd, 3rd, and 4th elements) and returns them as an array.

- Find following link for the Grep vs Filter in jQuery.
    

No comments:

Post a Comment