Skip to content

Instantly share code, notes, and snippets.

@b4dnewz
Created August 15, 2017 20:51
Show Gist options
  • Save b4dnewz/7a9af05ed9375db333780e229f113865 to your computer and use it in GitHub Desktop.
Save b4dnewz/7a9af05ed9375db333780e229f113865 to your computer and use it in GitHub Desktop.
Angular filter to include items that belong to a given array by array element value or optionally by object property value.
'use strict'
###*
# @ngdoc filter
# @name app.filter:inArray
# @description Angular filter to include items that belong to a given array
# by array element value or optionally by object property value.
# # inArray
###
angular.module 'app'
.filter 'inArray', ($filter) ->
(input, arr, prop) ->
# Exit if array is not valid or empty
if angular.isUndefined(arr) || !angular.isArray(arr) || arr.length == 0
return input
# Check if should filter by object property
if angular.isUndefined(prop)
$filter('filter')(input, (item) -> arr.indexOf(item) == -1)
else
$filter('filter')(input, (item) -> arr.indexOf(item[prop]) == -1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment