public
Last active

Using $.map as filter

  • Download Gist
mapAsFilter.js
JavaScript
1 2 3 4 5 6 7 8
// $.map can double as a filter operation in jQuery
// by returning null if you wish to remove the element.
 
// Only keep evens
var oneThruFive = [1,2,3,4,5];
var evens = $.map(oneThruFive, function(number) {
return number % 2 == 0 ? number : null;
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.