When working with JSON data, I often use the .filter method to remove certain categories and products from my dataset. Individually specifying each filter condition can quickly become lengthy and cumbersome.
For example, instead of writing out each exclusion criteria like this:
var myfilter = myjson.filter(function(c) {
return (c.category != 3 && c.category!= 5 && c.category!= 8 && c.category!= 11 && c.product != 191 && c.product != 139 && c.product != "string instead of int");
});
I would prefer a more concise approach using arrays for excluded categories and products:
var excludedcats = [3, 5, 8, 11]
var excludedproducts = [191, 139, "string instead of int"]
var myfilter = myjson.filter(function(c) {
return (excludedcats.includes(c.category) || excludedproducts.includes(c.product));
});
While I'm not an expert in JavaScript, I believe this could simplify the filtering process by using arrays and a single 'return' statement.
My research on this topic hasn't yielded the results I hoped for, but I did come across some interesting insights on array handling in JavaScript here.