Filtering With jQuery Grep


In this lesson we are going to learn about jQuery grep method, Grep method is use for filter the values by using jQuery, so let see an example first that by using grep method what can we do with code. Grep method demands for 3 things by default - 1. Pass Elements, 2. CallBack function, 3. Inverse who use return Boolean value that condition is true or false.

First look of Grep Method.

Image

Result

Image

In above example first we create an array with name arr and store 10 numbers into it and display this on console screen. Than we override arr variable for use jQuery grep method and pass all elements of arr array, than pass a call back function and into this we pass two arguments first is value of arr or we can say arr elements and second is index of arr elements. After passing these values we return call back function and set a condition that return the value which greater than 5 and display it on console screen.

We not use third parameter into above example which is inverse method who use as boolean value and as we know undefined value return the false value so in that case in last example when we not pass the inverse than it return false value let see further -- If we pass a true boolean value into above example than it return opposite of condition, i mean its return than function find a true value than inverse the condition by using this. -- Result : 1, 2, 3, 4, 5

Using Inverse(inv) The third Parameter.

Image

Use grep with Objects

Let make an filter function by using $.grep method who return only those name which contains last name is Kalra.

Image

Result

Image

We not pass any inv value which is third parameter and we know as who return boolean value so it return those names who contains last name is Kalra, if we pass here true as third parameter than it return all those name who not contains last name Kalra and display other name.

So this is very useful method for filter any type of data. Good bye! Keep learning.


Wconcert India © 2015   ·   All Rights Reserved

Free Web Hosting