Filtering Arrays with jQuery.grep - J Query

Problem
You need to filter and remove elements in an array.

Solution

Discussion
This recipe uses the $.grep() method to filter the months array so that it only includes entries that begin with the capital letter J. The $.grep method returns the filtered array.
The callback method defined by the developer takes two arguments and is expected to return a Boolean value of true to keep an element or false to have it removed. The first argument specified is the value of the array element (in this case, the month), and the second argument passed in is the incremental value of the number of times the$.grep() method has looped. So, for example, if you want to remove every other month,you could test whether ( i % 2 ) == 0, which returns the remainder of i / 2. (The % is the modulus operator, which returns the remainder of a division operation. So, when i = 4, i divided by 2 has a remainder of 0.)


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

J Query Topics