my example below works but it is not modular with a hard coded "switch" statement determining how many functions exist in my function array.
I have tried to figure out how to use forEach() and every() but I'm having troubles with passing parameters into the functions contained in the array. (At least that is my assumption at this point...
The following does not work?
_this.oData.forEach(function (oRow) {
function foo(checksArray) {
var meetsAll = fc.every(function (func,oRow) {
return func(oRow);
});
console.log(meetsAll);
if (meetsAll){
_this.oFiltered.push(oRow);
};
};
});
Here is a complete working sample but not right because not modular.
_this = this;
// sample data
_this.oData = [
{ 'itemfk': 123, 'vdrfk': 24 },
{ 'itemfk': 13, 'vdrfk': 34 },
{ 'itemfk': 18, 'vdrfk': 77 },
{ 'itemfk': 13, 'vdrfk': 24 },
{ 'itemfk': 48, 'vdrfk': 34 }
];
_this.oItemsSelected = [
{ 'itemfk': 123 },
{ 'itemfk': 13 }
];
_this.oVendorsSelected = [
{ 'vdrfk': 234 },
{ 'vdrfk': 24 }
];
// called by cascading controls
this.filterConditions = function () {
// build test conditions into array of functions
var fc = [];
// items condition
if (_this.oItemsSelected.length > 0) {
fc.push(
function (oRow) {
for (var nItem = 0; nItem < _this.oItemsSelected.length; nItem++) {
//console.log(_this.oItemsSelected[nItem].itemname +' row '+ oRow.itemname);
if (_this.oItemsSelected[nItem].itemfk === oRow.itemfk) {
return true;
}
};
return false;
}
);
};
// vendors condition
if (_this.oVendorsSelected.length > 0) {
fc.push(
function (oRow) {
for (var nVendor = 0; nVendor < _this.oVendorsSelected.length; nVendor++) {
if (_this.oVendorsSelected[nVendor].vdrfk === oRow.vdrfk) {
return true;
}
};
return false;
}
);
};
// loop data and apply conditions
_this.oFiltered = [];
_this.oData.forEach(function (oRow) {
switch (fc.length) {
case 1:
if (fc[0](oRow)) {
_this.oFiltered.push(oRow);
};
break;
case 2:
if (fc[0](oRow) && fc[1](oRow)) {
_this.oFiltered.push(oRow);
};
break;
};
});
// two oData rows (index zero and three) match conditions
console.log(_this.oFiltered);
};
Any help would be appreciated!
_this = this;
// sample data
_this.oData = [
{ 'itemfk': 123, 'vdrfk': 24 },
{ 'itemfk': 13, 'vdrfk': 34 },
{ 'itemfk': 18, 'vdrfk': 77 },
{ 'itemfk': 13, 'vdrfk': 24 },
{ 'itemfk': 48, 'vdrfk': 34 }
];
_this.oItemsSelected = [
{ 'itemfk': 123 },
{ 'itemfk': 13 }
];
_this.oVendorsSelected = [
{ 'vdrfk': 234 },
{ 'vdrfk': 24 }
];
// called by cascading controls
this.filterConditions = function () {
// build test conditions into array of functions
var fc = [];
// items condition
if (_this.oItemsSelected.length > 0) {
fc.push(
function (oRow) {
for (var nItem = 0; nItem < _this.oItemsSelected.length; nItem++) {
//console.log(_this.oItemsSelected[nItem].itemname +' row '+ oRow.itemname);
if (_this.oItemsSelected[nItem].itemfk === oRow.itemfk) {
return true;
}
};
return false;
}
);
};
// vendors condition
if (_this.oVendorsSelected.length > 0) {
fc.push(
function (oRow) {
for (var nVendor = 0; nVendor < _this.oVendorsSelected.length; nVendor++) {
if (_this.oVendorsSelected[nVendor].vdrfk === oRow.vdrfk) {
return true;
}
};
return false;
}
);
};
// loop data and apply conditions
_this.oFiltered = [];
_this.oData.forEach(function (oRow) {
switch (fc.length) {
case 1:
if (fc[0](oRow)) {
_this.oFiltered.push(oRow);
};
break;
case 2:
if (fc[0](oRow) && fc[1](oRow)) {
_this.oFiltered.push(oRow);
};
break;
};
});
// two oData rows (index zero and three) match conditions
console.log(_this.oFiltered);
};
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script src="https://stacksnippets.net/js"></script>
</head>
<body onload="filterConditions()">
</body>
</html>