Skip to content

Instantly share code, notes, and snippets.

@manute
Last active April 29, 2016 10:40
Show Gist options
  • Save manute/5318651 to your computer and use it in GitHub Desktop.
Save manute/5318651 to your computer and use it in GitHub Desktop.
Pluck Underscore.js function added to List Groovy metaClass
/*
One argument:
-propertyName
*/
def pluck = { propertyName ->
// Collect results filter coincidence by propertyName
delegate.collect { element ->
element."$propertyName"
}
}
// Add "pluck" to metaClass of List
List.metaClass.pluck = pluck
def users = [[name:"Joe",age:32],[name:'Jhon',age:20],[name:'Mary',age:25]]
assert users.pluck("name") == ["Joe","Jhon","Mary"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment