Wednesday, February 03, 2010

Using jQuery's grep to parse JSON data

jQuery's grep() is a powerful and underutilised filter method. It's a simple method which helps in searching/filtering data in arrays. Grep() is quite helpful when we have to parse JSON data. It's return type is an array.

http://api.jquery.com/jQuery.grep/
Description: Finds the elements of an array which satisfy a filter function. The original array is not affected.

Sample usage -

TO extract all employee ids.



ANother example on how to use grep() on JSON.

Consider sample JSON -


Suppose we need to extract only active employees from above JSON, we can filter the same using grep() and look for Active = True

Complete sample -

No comments: