Skip to content

Instantly share code, notes, and snippets.

@rcdilorenzo
Created August 17, 2012 15:14
Show Gist options
  • Select an option

  • Save rcdilorenzo/3379731 to your computer and use it in GitHub Desktop.

Select an option

Save rcdilorenzo/3379731 to your computer and use it in GitHub Desktop.
Ruby-Like select for Array
Array::select = (requestedElement) ->
count = {}
array = []
for element in this
count[element] = (count[element] || 0) + 1
if count[requestedElement]
for i in [1..count[requestedElement]]
array.push(requestedElement)
return array
else
return null
@rcdilorenzo
Copy link
Copy Markdown
Author

Note: Doesn't work for requesting an element's property in an array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment