JQuery. grep () function details

Source: Internet
Author: User
The jQuery. grep () function is used to filter elements in an array using the specified function and return the filtered array. The source array is not affected. The filter result is only reflected in the returned result array. This function is a global jQuery object. The jQuery. grep () function is used to filter elements in an array using the specified function and return the filtered array.

The source array is not affected. The filter result is only reflected in the returned result array.

This function is a global jQuery object.

Syntax

JQuery 1.0 adds this static function.

JQuery. grep (array, function [, invert])

Parameters

Note:

This function traverses the array elements and executes the filter function. It provides two parameters for function: one is the array element of the current iteration, and the other is the index of the current iteration element in the array.

If the invert parameter is not specified or the value is false, the result array contains all elements that function returns true. If the invert parameter is true, the result array contains all elements that function returns false.

Return Value

The Return Value of the jQuery. grep () function is of the Array type, and an Array of Results filtered by the specified function is returned.

Example & Description

The jQuery sample code of the jQuery. grep () function is as follows:

// Append the line feed label and the specified HTML content to the current page. function w (html) {document. body. innerHTML + ="
"+ Html;} var arr = [10, 25, 3, 0,-3, 20, 6, 8, 11]; function filter (value, index) {return value> 10;} var result = $. grep (arr, filter); // retains all elements greater than 10 w (result); //, 11var result2 = $. grep (arr, filter, true); // retains all elements not greater than 10 w (result2); //, 0,-, 8
Related Article

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.