How do I do this?
If I use the .where() formatter to only include orders that have id 71 I’ll not know if that order also had bought product 72.
I think I need the formatter “where does NOT contain”. But that doesn’t exist.
So then I think I need to use the .filter() formatter and create my own filter, but not sure how to use it.
I asked GitHub Copilot to solve this for you, the whereNot formatter for this use-case:
// Create a method that receives an array of objects, a property name and the respective value
// and returns all objects that don't have such property's value
exports.whereNot = function (arr, prop, value) {
return arr.filter((item) => {
return item[prop] !== value;
});
}
@ben my issue with this is that if I apply those in the suitable, it’ll just filter away the sbutable records. But the actual order object will still show up. Just the bought_products array is empty
exports.wherePropertyIncludesAndExcludes = function (arr, property_name, sub_property_name, include, exclude) {
return arr.filter((item) => {
let includeMatch = false;
let excludeMatch = false;
// Check if any of the properties match the include criteria
item[property_name].forEach((subElement) => {
if (include.includes(subElement[sub_property_name])) {
includeMatch = true;
}
});
// Check if any of the properties match the exclude criteria
item[property_name].forEach((subElement) => {
if (exclude.includes(subElement[sub_property_name])) {
excludeMatch = true;
}
});
// Return true if includeMatch is true and excludeMatch is false
return includeMatch && !excludeMatch;
});
}