Skip to content

Instantly share code, notes, and snippets.

@teebot
Created June 5, 2014 09:05
Show Gist options
  • Save teebot/e20ad1a824a25ebc4738 to your computer and use it in GitHub Desktop.
Save teebot/e20ad1a824a25ebc4738 to your computer and use it in GitHub Desktop.
any object by its id from a collection
employees = [{ id : 1, name : "tib" }, { id : 2, name : "dude" }]
search = (input, id) ->
for i in [0..input.length - 1]
if (input[i].id == id)
return input[i];
return null
console.log search(employees, 2).name
# as angular filter
# return any object by its id from a collection
app.filter 'getById', ->
return (input, id) ->
for i in [0..input.length - 1]
if (input[i].id == id)
return input[i];
return null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment