Last active
April 29, 2016 10:40
-
-
Save manute/5318651 to your computer and use it in GitHub Desktop.
Pluck Underscore.js function added to List Groovy metaClass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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