Skip to content

Instantly share code, notes, and snippets.

@rcdilorenzo
Created August 17, 2012 15:14
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 rcdilorenzo/3379731 to your computer and use it in GitHub Desktop.
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
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