I want to have code that can add dynamic filters to an array and then the array run through a .filter that then checks if all the filters are true:
var checks = [item =>item.indexOf("e") == -1, item =>item.indexOf("r") == 0];
var words = ["roast", "wings", "chive", "pests", "rhyme"]
var filteredArr = words.filter(item => checks.every(f => f(item)));
I want it to take two inputs like "r" and the column it should be in (0) and add item =>item.indexOf("r") == 0
to the checks array.
The problem I have been running into is that when creating arrow functions to push into the checks array, the variables don't compute now because it is an arrow function. Ex:
var value = "r"
item =>item.indexOf(value) == -1
does not work because it will stay as value and not the value the variable is.
var value = "r"
var checks = [item =>item.indexOf(value) == -1];
var words = ["roast", "wings", "chive", "pests", "rhyme"]
var filteredArr = words.filter(item => checks.every(f => f(item)));
console.log(filteredArr);
I have looked through many other questions but they never are able to add filters like I am describing. I was wondering if there is a fix for this problem or another way to add checks to an array dynamically.
Edit:
var words = ["roast", "wings", "chive", "pests", "rhyme"];
var checks = [];
for(let i = 0; i < 30; i++){
var value = "r"
var column = i%5;
checks.push(item =>item.indexOf(value) == column);
}
var filteredArr = words.filter(item => checks.every(f => f(item)));
console.log(filteredArr);
Is it possible for the filter that I pushed to include the i%5 value?