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.

6
  • Can you give us an example that fails? aka, make the code above actually function with the error. Commented Feb 9, 2018 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 .... Commented Feb 9, 2018 at 17:45
  • @Paulpro This works, thank you! Was going off documentation for ...values but I suppose I misunderstood. Commented Feb 9, 2018 at 17:49
  • This function does not return anything. You might look at Array.prototype.every. Commented Feb 9, 2018 at 18:01
  • 1
    Possible duplicate of How to check if a string contains all the elements of an array in Javascript Commented Feb 9, 2018 at 18:56

2 Answers 2

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']);

Sign up to request clarification or add additional context in comments.

2 Comments

This is the correct answer. Cannot upvote but you saved me from more headache.
You can mark it green to depict this was helpful to you. Glad to help you.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.