I am struggling with filtering an array of partners. Just for the sake of simplicity, I am defining partner object as below:
var partners = [
{ partnerCategory: "Gold", programs: ["Enterprise"] },
{ partnerCategory: "Silver", programs: ["Core", "Enterprise"] },
{ partnerCategory: "Silver", programs: ["Enterprise"] }
];
Now there are 2 picklists: partnerCategory and programs. If the user selects any value from the picklist, then I am populating an object filterCriteria.
Now I am defining filterCriteria in various ways to get an idea of the records that should be returned.
OPTION 1
var filterCriteria = [
{ fieldName: "partnerCategory", fieldValue: ["Silver"] }
];
2nd and 3rd record should be returned.
OPTION 2
var filterCriteria = [
{ fieldName: "partnerCategory", fieldValue: ["Silver", "Gold"] }
];
No record should be returned
OPTION 3
var filterCriteria = [
{ fieldName: "partnerCategory", fieldValue: ["Silver"] },
{ fieldName: "programs", fieldValue: ["Enterprise"] }
];
2nd and 3rd records should be returned.
OPTION 4
var filterCriteria = [
{ fieldName: "partnerCategory", fieldValue: ["Silver"] },
{ fieldName: "programs", fieldValue: ["Enterprise", "Core"] }
];
Only 2nd record should be returned.
So basically if a value is present in filterCriteria object, then the record should have all the values present in the filter.
I am using the below code to get the output, but I am missing something:
var result = partners.filter(function (obj) {
return filterCriteria.every(function (c) {
var value = obj[c.fieldName];
if (typeof value === 'object') {
return Object.keys(value).every(function (key) {
return c.fieldValue.indexOf(value[key]) > -1
})
}
else
return c.fieldValue.indexOf(value) > -1
})
});