Last active
December 11, 2015 20:59
-
-
Save crystalschang/4659561 to your computer and use it in GitHub Desktop.
Filtering function for stock rows
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
/** Returns a new array of valid stock rows, given the input selections | |
* slimStockArray: the starting list of stocks, to be filtered | |
* keyOrder: mapping of keys to its index in slimStockArray, ex: {'color':0,'d3':1,'d4':2,'onhand':3} | |
* selections: object with the current user's selections, ex: {'color':'3', 'd4':'60425'} | |
**/ | |
function filter(slimStockArray, keyOrder, selections) { | |
var result = new Array(); | |
for(var i=0; i<slimStockArray.length; i++) { | |
var row = slimStockArray[i]; | |
var valid = true; | |
for(key in selections) { | |
// determine the array index for the given selection key | |
var idx = keyOrder[key]; | |
var val = row[idx]; | |
// check whether the user's selection matches the current stock row's value | |
var selectedVal = selections[key]; | |
if(val!==selectedVal) { | |
valid = false; | |
} | |
} | |
if (valid) { | |
result.push(row); | |
} | |
} | |
return result; | |
} | |
// If the customer further narrows down their selection, you could use the returned value as the input for the next slimStockArray | |
// so that you don't have to filter through the entire full list again | |
// This could also be modified to just return a list of the various sets of valid values, given the selections already made | |
{ | |
color: | |
{ '136': true, | |
'1413': true, | |
'4885': true, | |
'15874': true } | |
d4: | |
{ '60425': true} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment