I have an array which is constructed using user input values and can have any length:
var products = [
{key1: string1,
key2: 1,
key3: 30,
key4: 1},
{key1: string2,
key2: 2,
key3: 35,
key4: 2}
]
And I have some filters
var filters = {
key2: 1,
key3: {min: 20, max: 40},
key4: 1,
}
I'm currently using the below function to filter products using my filters
function multiFilter(array, filters) {
const filterKeys = Object.keys(filters);
return array.filter((item) => {
// flipped around, and item[key] forced to a string
return filterKeys.every(key => !!~String(item[key]).indexOf(filters[key]));
});
}
var filtered = multiFilter(products, filters);
console.log(filtered);
Everything works as expected when key3 in filters has an exact number, but I'm running into issues when trying to incorporate a range. How would I amend the function to allow for a range in key3 (filters)?
indexOffor detection