This is my preferred approach.
I like this method because it allows for easy addition of objects to the items
array and enables dynamic filtering.
function getFilteredItems(myItems) {
var items = [{name:'item1'}, {name:'item2'}];
if( myItems.reduce((c, item) => c += items.map(obj => obj.name).includes(item.name) , 0) == 2 ) { items.shift(); };
return myItems.filter(item => !items.map(obj => obj.name).includes(item.name) )
}
If the items
array doesn't contain objects
function getFilteredItems(myItems) {
var items = ['item1', 'item2'];
if( myItems.reduce((c, item) => c += items.includes(item.name) , 0) == 2 ) { items.shift(); };
return myItems.filter(item => !items.includes(item.name) )
}
EDIT: Using standard functions instead of arrow functions
// When items are objects
function getFilteredItems(myItems) {
var items = [{name:'item1'}, {name:'item2'}];
if( myItems.reduce(function(c, item) { return c += items.map( function(obj) { return obj.name}).includes(item.name)} , 0) == 2 ) { items.shift(); };
return myItems.filter( function(item) { return !items.map( function(obj) {return obj.name}).includes(item.name)} )
}
// When items are strings
function getFilteredItems(myItems) {
var items = ['item1', 'item2'];
if( myItems.reduce(function(c, item){ return c += items.includes(item.name) } , 0) == 2 ) { items.shift(); };
return myItems.filter( function(item) { return !items.includes(item.name)} )
}