Introduction to the Grep () method of Jquery traversal array

Source: Internet
Author: User

The grep () method is used for array element filtering filtering.

grep (Array,callback,boolean); method parameters are described.

Array---pending arrays

Callback---This callback function is used to process each element in the array and filter the element, which contains two parameters, the first parameter is the value of the current array element, the second parameter is the subscript of the current array element, and the return value is a Boolean value.

Here is the source code for the grep () method:

    //grep function, the third parameter indicates whether to reverse the results of FN! Grep:function(Elems, callback, invert) {varCallbackinverse, Matches=[], I= 0,      //Save Array NumberLength =Elems.length,//reverse the second parameter passed in, and true turns False,false into true    //If invert is false, that is,!inverse is true, then the element directly returning the function to True is added, so the desired callbackexpect is trueCallbackexpect =!invert; //Go through the array, only saving the items    //That pass the validator function     for(; i < length; i++ ) {      //if invert is false to indicate no inversion, then if FN returns True, it is added directly to the matches array    //gets the inverse result of the return value of the callback function resultCallbackinverse =!callback (elems[i], i); //This code indicates that the return value of the callback function is bool if(Callbackinverse!==callbackexpect)      {Matches.push (elems[i]); }      }      returnmatches; }  

Invert---The default is false, when the value is False, the array returned by grep () is an array that satisfies the callback condition, and when True, the grep () method returns an array that does not satisfy the callback condition.

When the value of invert is true. The code is as follows:

        $ (function  () {            varfunction  (value, index) {                return Value > 5;            },true);            alert (result);    
result=[0,1,2,3,4,5]

When the value of invert is false. The code is as follows
      $ (function  () {            varfunction  (value, index) {                return Value > 5;            },false);            alert (result);        });

RESULT=[6];

When the value of invert is not filled, the default is False.

Introduction to the Grep () method of Jquery traversal array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.