Skip to content

Commit

Permalink
finished itemExcluder.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKarlsson committed Aug 14, 2018
1 parent c312333 commit 126f7dd
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/itemExcluder.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,53 @@
// implement a method to effieciently exlude items from the items array, as outlined in the exlude array.
// Implement a method to effieciently exlude items from the items array, as outlined in the exlude array.

const items = [
const itemList = [
{type: 'computer', color: 'green', flavor: 'apple'},
{type: 'cellphone', color: 'gold', flavor: 'orange'},
{type: 'computer', color: 'silver', flavor: 'grape'},
]

const exclude = [
const excludeList = [
{k: 'type', v: 'cellphone'},
{k: 'color', v: 'silver'},
{k: 'color', v: 'gold'},
];

const itemExcluder = (items, exlude) => {
const excludeCache = {};

// Build exclude cache
exlude.forEach(item => {
if (excludeCache.hasOwnProperty(item.k)) {
excludeCache[item.k][item.v] = true;
} else {
const newExludeObj = {};
newExludeObj[item.v] = true
excludeCache[item.k] = newExludeObj;
}
});



// Check if any items are in the xclude cache
const filteredItems = [];

items.forEach(item => {
let filterFlag = false;

Object.keys(item).forEach(itemKey => {
if (excludeCache[itemKey]) {
if (excludeCache[itemKey][item[itemKey]] ) {
filterFlag = true
}
}
});
if (!filterFlag) {
return filteredItems.push(item);
}
});

return filteredItems;
}

const result = itemExcluder(itemList, excludeList);
console.log('result', result);

0 comments on commit 126f7dd

Please sign in to comment.