Created
August 15, 2017 20:51
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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