Home >Web Front-end >JS Tutorial >Simple Tip: Parsing jQuery - Grep

Simple Tip: Parsing jQuery - Grep

WBOY
WBOYOriginal
2023-08-28 17:01:091305browse

简单技巧:解析jQuery - Grep

In the latest episode of "Anatomy of jQuery," we'll lift the lid on $.grep and see exactly what's going on behind the scenes.

Subscribe to our YouTube page to watch all video tutorials!

grep jQuery source

// jQuery source for the grep method 
grep: function( elems, callback, inv ) {
		var ret = [];

		// Go through the array, only saving the items
		// that pass the validator function
		for ( var i = 0, length = elems.length; i < length; i++ ) {
			if ( !inv !== !callback( elems[ i ], i ) ) {
				ret.push( elems[ i ] );
			}
		}

		return ret;
	}

Hopefully you now have a better understanding of what's going on behind the scenes. Remember: any time you need to remove items from an array, $.grep will do the job just fine!

The above is the detailed content of Simple Tip: Parsing jQuery - Grep. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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