Quick Tip: Dissecting jQuery – Grep
Tutorial Details
- Topic: jQuery Source
- Difficulty: Intermediate
- Format: 7 Minute Screencast
In this latest episode of “Dissecting jQuery,” we’ll peal back the cover of $.grep, and learn exactly what’s happening behind the scenes.
jQuery Source for grep
// 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 happening behind the scenes. Just remember: any time that you need to remove items from an array, $.grep will do the trick quite nicely!
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
- Charles
- http://dragongears.com dragongears
- Mdobs
- http://www.facebook.com/Dev.MZahran Mohamed Zahran
- http://zimg.me Zach
- Bogaert Dieter
- Leigh Kaszick
- Bogaert Dieter
- Leigh Kaszick
- nadeem khedr
- http://www.code.my devlim
- http://lookitsatravis.com Travis Vignon
- Nasi
- http://datasplash.co.uk Darren Lunn
- http://www.calendee.com Justin Noel
- yassir
- http://www.webdesigncrap.info Gary
- passerby
- alvaro obyrne
- http://www.umbraprojekt.pl mingos
- http://net.tutsplus.com Jeffrey Way
- http://ezror.com sunny
- alvaro obyrne
- Bjarki
- http://blueco.ir/ طراحی سایت و سئو
- www.isatise.com
