Home  >  Article  >  CMS Tutorial  >  Quick tip: A closer look at jQuery - Grep

Quick tip: A closer look at jQuery - Grep

王林
王林Original
2023-08-28 17:49:101230browse

快速技巧:深入剖析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 Quick tip: A closer look at 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
Previous article:Starting with Backbone.jsNext article:Starting with Backbone.js