0

I have a JS function that creates an array based on the filters the user selects (this part works fine), and then I have another function (below) which loops through this array and should return true if the value is contained in the string and false if not.

var testString = "winter casual";
var valArray = ["winter", "casual"];


function filterArray(...values){ 
  for (i=0; i<values.length; i++){
    if (testString.includes(values[i])){
      console.log(true);
    } else {
      console.log(false);
    }
  }

 filterArray(valArray);

However, I click 1 filter button to add "winter" to the values array and it returns "true", but then I click another to add casual and it returns false, even though casual is included in the string as well. I have console logged values[] to ensure that both "winter" and "casual" are in my array, and they are.

Brett
  • 1
  • 1
  • Can you give us an example that fails? aka, make the code above actually function with the error. – epascarello Feb 09 '18 at 17:41
  • 1
    Are you passing an array into that function? If so you should not use `...` it should just be `function filterArray( values ) {`. If you call it as `filterArray('winter','casual')` you need the `...`, but if you call it as `filterArray(['winter','casual'])` you need to ***not*** have the `...`. – Paul Feb 09 '18 at 17:45
  • @Paulpro This works, thank you! Was going off documentation for ...values but I suppose I misunderstood. – Brett Feb 09 '18 at 17:49
  • This function does not return anything. You might look at [`Array.prototype.every`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every). – Scott Sauyet Feb 09 '18 at 18:01
  • @Brett `...values` creates an array called values from the function call's individual arguments. If an argument is an array that means you end up with a 2D array. You would have `values = [['winter','casual']];` which has only one element (so you were only seeing one logged value) and that element is an array. Then you end up calling `testString.includes(['winter','casual'])`. You would find that it would output true (once) if your string was `winter,casual` with a comma because the `toString()` of the array would match the entire `testString`. – Paul Feb 09 '18 at 18:14
  • 1
    Possible duplicate of [How to check if a string contains all the elements of an array in Javascript](https://stackoverflow.com/questions/40649681/how-to-check-if-a-string-contains-all-the-elements-of-an-array-in-javascript) – Heretic Monkey Feb 09 '18 at 18:56

2 Answers2

0

Replace ...values with values as the parameter:

var testString = "winter casual";

function filterArray(values){ 
  for (i=0; i<values.length; i++){
    if (testString.includes(values[i])){
      console.log(true);
    } else {
      console.log(false);
    }
  }
}
      
filterArray(['winter']);

filterArray(['winter', 'casual']);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

I'm not quite sure if this is what you're looking for, as your function doesn't actually return anything, but this is a fairly simple function that might be useful:

const containsAll = (str, strs) => strs.every(s => str.includes(s))

console.log(containsAll("winter casual", ['winter'])) //=> true
console.log(containsAll("winter casual", ['winter', 'casual'])) //=> true
console.log(containsAll("winter casual", ['winter', 'casual', 'medium'])) //=> false
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103