Home Web Front-end JS Tutorial Detailed explanation of jQuery.grep() function

Detailed explanation of jQuery.grep() function

Dec 13, 2016 pm 02:57 PM
grep

jQuery.grep() function is used to filter the elements in the array using the specified function and return the filtered array.

The source array will not be affected, and the filtered results are only reflected in the returned result array.

This function belongs to the global jQuery object.

Syntax

jQuery 1.0 adds this static function.

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

Parameters

Detailed explanation of jQuery.grep() function

Notes:

This function will iterate through the array elements and execute the filter function. It will provide two parameters to the 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 is false, the resulting array will contain all elements for which the function returns true. If the parameter invert is true, the resulting array will contain all elements for which the function returned false.

Return value

The return value of the jQuery.grep() function is of Array type and returns the result array filtered by the specified function.

Example & Description

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

//在当前页面内追加换行标签和指定的HTML内容

function w( html ){

    document.body.innerHTML += "<br/>" + html;

}

 

 

var arr = [ 10, 25, 3, 0, -3 , 20, 6, 8, 11 ];

 

function filter(value, index){

    return value > 10;

}

 

var result = $.grep( arr, filter );

// 保留了所有大于10的元素

w( result ); // 25,20,11

 

 

var result2 = $.grep( arr, filter, true );

// 保留了所有不大于10的元素

w( result2 ); // 10,3,0,-3,6,8

Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use grep command for log analysis in Linux? How to use grep command for log analysis in Linux? Jul 29, 2023 pm 02:12 PM

How to use grep command for log analysis in Linux? Introduction: Logs are important records generated during system operation. For system operation, maintenance and troubleshooting, log analysis is an essential task. In the Linux operating system, the grep command is a powerful text search tool that is very suitable for log analysis. This article will introduce how to use the grep command commonly used for log analysis and provide specific code examples. 1. Introduction to grep command grep is a file in Linux system

How to use the grep command to query the contents of a specific first and last file How to use the grep command to query the contents of a specific first and last file Jan 09, 2024 am 09:45 AM

If you want to view the contents of a file in a LINUX system, how can you view the first and last contents of a specified file? Let's take a look at the tutorial on using grep to query the contents of the specified first and last files. 1. Open LINUX, here we can use the UBUNTU operating system. 2. Find TERMINAL on the left taskbar and open the terminal window. 3. Add ^ after grep to specify the beginning of a certain line. 4. Add $ after the content after grep to specify the end of a certain line. 5. ^$ can be used at the same time to specify the necessary content at the beginning and end of a certain line. 6. If a character in the middle is missing or incorrect, it will not be queried. 7. Use with -i to ignore the case rules. 8. You can also use -v

Introducing how to use the grep command to display context information under Linux Introducing how to use the grep command to display context information under Linux Jan 04, 2024 pm 04:41 PM

This article mainly studies the related content of grep displaying several lines of information before and after under Linux, as follows. Grep under standard unix/linux controls the context through the following parameters. grep-C5foofile displays the line matching the foo string in the file file and the upper and lower 5 lines. grep-B5foofile displays foo and the first 5 lines. grep-A5foofile displays foo and the last 5 lines to view the grep version. The method of numbering is grep-V. If you want to upgrade, the upgrade method: the latest source code (google or Baidu search homepage), compile and install it somewhere, such as /home/aaa/bin/, then use / when using it in the future. home/aaa/

What is the syntax of Linux grep command What is the syntax of Linux grep command May 14, 2023 pm 10:43 PM

The Linuxgrep command is used to find strings that meet conditions in files. The grep command is used to find files whose content contains the specified template style. If the content of a file is found to match the specified template style, the default grep command will display the column containing the template style. If no file name is specified, or the file name is -, the grep command reads data from the standard input device. Syntax: grep parameters: -a or --text: Do not ignore binary data. -A or --after-context=: In addition to displaying the column that conforms to the template style, and display the content after the line. -b or --byte-offset: before displaying the line that matches the style

How to use grep in Linux system? How to use grep in Linux system? Feb 19, 2024 pm 03:54 PM

In Linux, grep is a very common and important tool. It is a command that every professional operation and maintenance engineer must master, because it can quickly find and filter the contents of files. So how to use grep in Linux system? The following is Let’s take a look at the common usage introduction. 1. Basic usage The grep command is mainly used to search for lines of a specified pattern in files. For example, to find the line containing "example" in the file file.txt, you can use the grep command. grep 'example' file.txt grep will output all lines containing 'example'.​

Regular usage of grep Regular usage of grep Nov 16, 2023 am 10:29 AM

The regular usage of grep includes: 1. Simple matching; 2. Basic regular expressions; 3. Use of metacharacters; 4. Use of anchor characters; 5. Use of character classes; 6. Use of quantifiers. Detailed introduction: 1. Simple matching, use the grep command followed by the string to be matched; 2. Basic regular expressions, use the -E option to enable extended regular expression functions; 3. The use of metacharacters, in regular expressions , you can use some metacharacters to represent specific characters or character sets; 4. The use of anchor characters, etc.

What does the Linux grep command do? What does the Linux grep command do? May 29, 2023 am 08:43 AM

The Linuxgrep command is used to find strings that meet conditions in files. The grep command is used to find files whose content contains the specified template style. If the content of a file is found to match the specified template style, the default grep command will display the column containing the template style. If no file name is specified, or the file name is -, the grep command reads data from the standard input device. Syntax: grep parameters: -a or --text: Do not ignore binary data. -A or --after-context=: In addition to displaying the column that conforms to the template style, and display the content after the line. -b or --byte-offset: before displaying the line that matches the style

What are the uses of grep in linux? What are the uses of grep in linux? Sep 05, 2023 am 11:01 AM

The usage of grep in Linux includes basic usage, ignoring case, regular expression search, reverse search, counting the number of matching lines, recursive search, output line number and searching from the input stream, etc. Detailed introduction: 1. Basic usage. The basic usage of the grep command is to find lines containing the specified pattern in the file. To find the lines containing "example" in the file file.txt, you can execute the command "grep "example" file.txt "; 2. Ignore case. By default, grep is case-sensitive and so on.

See all articles