Created
August 12, 2016 08:27
-
-
Save jmosul/1e2fd93222778a3f712c6843e7ba1514 to your computer and use it in GitHub Desktop.
Filter that filters whether an object does/doesn't appear in another array (uses underscore.js)
This file contains hidden or 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'; | |
angular.module('dmn.filters.inCollection', []) | |
/* | |
* Filters an array of objects (collection) against another collection. | |
* | |
* @param compareCollection the collection to compare against | |
* @param compareField the field in both collections to compare | |
* @param filterOnIsIn Boolean of whether to filter against 'in collection' TRUE, 'not in collection' FALSE. Defaults to TRUE (is in collection) | |
*/ | |
.filter('inCollection', function() { | |
return function (items, compareCollection, compareField, filterOnIsIn ){ | |
filterOnIsIn = angular.isDefined(filterOnIsIn) ? filterOnIsIn : true; | |
// array to hold filtered | |
var filteredItems = []; | |
angular.forEach( items, function ( item ) { | |
var comparison = {}; | |
var found = false; | |
if( angular.isDefined(item[compareField]) ){ | |
comparison[ compareField ] = item[ compareField ]; | |
found = _.findIndex( compareCollection, comparison ) > -1; | |
} | |
// if found matches the test add it into the the filtered list | |
if( found === filterOnIsIn ){ | |
filteredItems.push( item ); | |
} | |
}); | |
return filteredItems | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment