-2

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?

  • `item =>item.indexOf(value) == -1` should work just fine. And did you mean to use string literals for your `words` array, or are those all independent variables? (If so, please edit to show what they contain) – CertainPerformance Jan 19 '22 at 23:17
  • As you can see, the code works. – CertainPerformance Jan 19 '22 at 23:21
  • @CertainPerformance Instead of adding item =>item.indexOf(value) == -1 i want it to add item =>item.indexOf("r") == -1. I want it to add the check based on the variable value. So if the variable value was "n" it would create a check: item =>item.indexOf("n") == -1 – Philyshark7 Jan 19 '22 at 23:23
  • That will work. If you have `value = "n"`, that is the check it will carry out. – CertainPerformance Jan 19 '22 at 23:24
  • @CertainPerformance yes but I want it to not have the variable value in the check so that if the value variable changes to make a new check, the other check would still work – Philyshark7 Jan 19 '22 at 23:27

1 Answers1

-1

Your code works for me. Please explain how this doesn't work for you.

var value = "e";

var checks = [item =>item.indexOf(value) == -1, item =>item.indexOf("r") == 0];   
var words = ["roast", "wings", "chive", "pests", "rhyme"]

// prints roast
console.log("answer:"+words.filter(item => checks.every(f => f(item))));

value = "o";

// prints rhyme
console.log("answer:"+words.filter(item => checks.every(f => f(item))));

BTW are you making a wordle solver? Because I made one just this morning in Python ;)

Mike Clark
  • 10,027
  • 3
  • 40
  • 54
  • yes lmao :D But if this code was in a for loop or something and the value variable was not always going to stay the same it would not work, is there a way for it to add the check with no variable name and just the value? – Philyshark7 Jan 19 '22 at 23:30
  • @Philyshark7 variable capture in JS closures is complicated. Try switching your for-loop to use `let` instead of `var`. See: https://stackoverflow.com/a/750506/312407 – Mike Clark Jan 19 '22 at 23:32
  • And by the way, it's important to post a complete working program that reproduces your problem. If your original problematic code has a for-loop, your Question should contain that same for-loop, etc. – Mike Clark Jan 19 '22 at 23:33
  • Thanks, added a for loop example. My question was if it was possible to add the check with no variable name and the value of the variable instead and the answers I got were that it was already working which is not what I asked. The for loop is an example to show why having the value in the name would be a problem. – Philyshark7 Jan 19 '22 at 23:55
  • it matters if you declare the variable as `var value` vs `let value`. You used neither -- I'm not sure what that does. – Mike Clark Jan 19 '22 at 23:58
  • Let doesn't fix the problem I am trying to fix, that the i value will not be part of the check – Philyshark7 Jan 20 '22 at 00:02