coffeescript Arrays Filtering values


Example

theUsers = [
  {id: 1, username: 'john'}
  {id: 2, username: 'lexy'}
  {id: 3, username: 'pete'}
]

To retain only users whose id is greather than 2, use the following:

[{id: 3, username: 'pete'}]

Method 1 - using .filter

filteredUsers = theUsers.filter (user) -> user.id >= 2

Method 2 - using comprehension

filteredUsers = (user for user in theUsers when user.id >= 2)