Skip to content

Instantly share code, notes, and snippets.

@dominiek
Created October 8, 2009 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominiek/204843 to your computer and use it in GitHub Desktop.
Save dominiek/204843 to your computer and use it in GitHub Desktop.
// For all you Rubyists out there, jQuery has a couple "hidden" methods
// for working with arrays that work and are named just like Ruby, "grep" and "map":
foo = [1, 2, 3, 4, 5]
/*
Ruby way:
foo.grep do |value|
value > 2 && value < 5
end
*/
// jQuery way:
$.grep(foo, function(value, index){
return value > 2 && value < 5;
});
// -> [3, 4]
/*
Ruby way:
foo.map do |value|
value * 2
end
*/
// jQuery way:
$.map(foo, function(value, index){
return value * 2;
});
// -> [2, 4, 6, 8, 10]
// This jQuery tip has been brought to you by Paul Bakaus in the POOL.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment