I have not used the $.grep () method before, and the $.grep () method filters the array according to a certain condition, so using the $.grep () method can filter out the results we want from the array. Let's look at an example with an array named Nums:
var nums = ' 1,2,3,4,5,jquery,css,5 '. Split (', ');
There are numbers and strings in the visible array, and if we want to find the strings in them, we can use the $.grep () method directly to accomplish this task, as follows:
function (num, index) { // num = current value of the array element // index = subscript of the current value return // The result is: ["JQuery", "CSS" ]
It's easy to think of the $.map () method, the $.map () method can convert an array to another array, so using the $.map () method can also accomplish this task, as follows:
function (num, index) { // and $.grep () difference //return IsNaN, get result: [True, True] returnnull//
This article briefly introduces the use of the lower $.grep () method, and compares the next $.map () method. As you can see, the proper use of JQuery's built-in approach simplifies our programs.
jquery uses the $.grep () method