-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c312333
commit 126f7dd
Showing
1 changed file
with
44 additions
and
3 deletions.
There are no files selected for viewing
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
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); |