I have come up with this list of categories and keywords by which the products should be categorised. You may check it below:
const categories = {
"Meso": ["snicla", "šnicla", "krmenadla", "pileca prsa", "belo meso", "pileća prsa", "krilca"],
"Čokolada": ["čokolada", "cokolada"],
"Majonez": ["majonez"],
"Kečap": ["kečap", "kecap"],
"Senf": ["senf"],
"Paradajz": ["paradajz"],
"Kobasice": ["kobasica"],
"Posebne kobasice / Salame": ["posebna kobasica", "pileca posebna", "salama"],
"Pileća prsa": ["pileća prsa", "pileca prsa"],
"Pašteta": ["pašteta", "pasteta"],
"Ćevapi": ["ćevapi", "cevapi", "ćevapčići"],
"Šnicla": ["šnicla"],
"Krilca": ["krilca"],
"Voće": ["banana", "banane", "jabuka", "jabuke", "mandarine"],
"Sokovi": ["borovnica", "borovnice", "jabuka", "jabuke", "narandža", "pomorandže", "breskva", "breskve"],
"Kuhinjski aparati": ["Ketler za vodu", "Seckalica MC 400"],
"Za domaćice": ["daska za peglanje", "bokal", "čistač", "cistac",],
"Vileda": ["vileda"],
"Filtriranje vode": ["Akvafor", "Akvaphor"],
"Alati": ["metla", "auto set"],
"Maziva": ["majonez", "kečap", "kecap", "senf", "sos"]
};
Now, what I need is to exclude certain keywords from some categories. For example, I have a category “Voće” (eng. fruit) and inside of that category I want to have a “banana” keyword included, but I don’t want to have “choco banana” or “krem banana” included in that category (since it isn’t a fruit but a candy). The solution I’ve tried you may find below:
// Function to filter products based on exclusion rules
function filterProducts(category, exclusions) {
return categories[category].filter(product => {
let lowerCaseProduct = product.toLowerCase();
// Check if product does not include any exclusion keyword
return !exclusions.some(exclusion => lowerCaseProduct.includes(exclusion));
});
}
// Example usage: Exclude "Krem banana" from the "Voće" category if found as a keyword
let excludedKeywords = ["krem banana"];
categories["Voće"] = filterProducts("Voće", excludedKeywords);
console.log(categories["Voće"]);
Unfortunately, I couldn’t manage to get the desired result with this piece of code.
2
Seems like you need to split()
the exclusion
and then add another some()
so you check each word separately.
This way banane
is removed as expected.
const categories = {"Meso": ["snicla", "šnicla", "krmenadla", "pileca prsa", "belo meso", "pileća prsa", "krilca"], "Čokolada": ["čokolada", "cokolada"], "Majonez": ["majonez"], "Kečap": ["kečap", "kecap"], "Senf": ["senf"], "Paradajz": ["paradajz"], "Kobasice": ["kobasica"], "Posebne kobasice / Salame": ["posebna kobasica", "pileca posebna", "salama"], "Pileća prsa": ["pileća prsa", "pileca prsa"], "Pašteta": ["pašteta", "pasteta"], "Ćevapi": ["ćevapi", "cevapi", "ćevapčići"], "Šnicla": ["šnicla"], "Krilca": ["krilca"], "Voće": ["banana", "banane", "jabuka", "jabuke", "mandarine"], "Sokovi": ["borovnica", "borovnice", "jabuka", "jabuke", "narandža", "pomorandže", "breskva", "breskve"], "Kuhinjski aparati": ["Ketler za vodu", "Seckalica MC 400"], "Za domaćice": ["daska za peglanje", "bokal", "čistač", "cistac",], "Vileda": ["vileda"], "Filtriranje vode": ["Akvafor", "Akvaphor"], "Alati": ["metla", "auto set"], "Maziva": ["majonez", "kečap", "kecap", "senf", "sos"] };
function filterProducts(category, exclusions) {
return categories[category].filter(product => {
let lowerCaseProduct = product.toLowerCase();
// Check if product does not include any exclusion keyword
return !exclusions.some(exclusion => exclusion.split(' ').some(ex => lowerCaseProduct.includes(ex)));
});
}
// Example usage: Exclude "Krem banana" from the "Voće" category if found as a keyword
let excludedKeywords = ["krem banana"];
console.log('Before', categories["Voće"]);
categories["Voće"] = filterProducts("Voće", excludedKeywords);
console.log('After', categories["Voće"]);
1