Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

How can I check if a string contains any element of an array? I want to filter some array if the element has some string. Please see below code.

var arr = ['banana', 'monkey banana', 'apple', 'kiwi', 'orange'];

function checker(value) {
  var prohibited = ['banana', 'apple'];

  for (var i = 0; i < prohibited.length; i++) {
    if (value.indexOf(prohibited[i]) == -1) {
      return true;
    } else {
      return false;
    }
  }
}

arr = arr.filter(checker);
console.log(arr);

The result is [ 'apple', 'kiwi', 'orange' ]. The 'apple' should get removed, but it isn't.

Above code only filtered 'banana', not 'apple'. I have many keywords to filter. Is there an easier way?

share|improve this question
up vote 10 down vote accepted

Problem lies in the for loop, which only iterates once since return ends the function, cutting off the for loop in the process. So, you can update the code like so to make the function only return once the for loop has been completed .

var arr = ['banana', 'monkey banana', 'apple', 'kiwi', 'orange'];

function checker(value) {
  var prohibited = ['banana', 'apple'];

  for (var i = 0; i < prohibited.length; i++) {
    if (value.indexOf(prohibited[i]) > -1) {
      return false;
    }
  }
  return true;
}

arr = arr.filter(checker);
console.log(arr);


For reducing the function you can use every() and indexOf() methods

The 'every' method executes the provided callback function once for each element present in the array until it finds one where callback returns a falsy value (a value that becomes false when converted to a Boolean). If such an element is found, the every method immediately returns false. Otherwise, if callback returned a true value for all elements, every will return true. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.(Taken from here)

var arr = ['banana', 'monkey banana', 'apple', 'kiwi', 'orange'];

function checker(value) {
  var prohibited = ['banana', 'apple'];
  return prohibited.every(function(v) {
    return value.indexOf(v) == -1;
  });
}

arr = arr.filter(checker);
console.log(arr);


For older browser check polyfill option of every method.


You could even use a regex here. Generate regex using the array and use test() to check match

var arr = ['banana', 'monkey banana', 'apple', 'kiwi', 'orange'];

function checker(value) {
  var prohibited = ['banana', 'apple'];
  var regex = new RegExp(prohibited.map(function(s) {
    return s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
  }).join('|'));
  return !regex.test(value);
}

arr = arr.filter(checker);
console.log(arr);

Refer this answer for string to regex conversion : Can you create JavaScript regexes on the fly using string variables?

share|improve this answer

It can be as simple as that:

const arr = ['banana', 'monkey banana', 'apple', 'kiwi', 'orange']

const checker = value=>
  !['banana', 'apple'].some(element=> value.includes(element))

console.log(arr.filter(checker))

ECMAScript 6 FTW!

The checker uses an arrow function.

The ! means that it will exclude all elements that doesn't meet the conditions.

The some() method tests whether some element in the array passes the test implemented by the provided function.

from Array.prototype.some() docs on MDM

The includes() method determines whether one string may be found within another string, returning true or false as appropriate.

from String.prototype.includes() docs on MDM


As some latest ECMAScript features aren't supported in all browsers, you should use Babel to compile your code to ECMAScript 5.

share|improve this answer
<script>
$(document).ready(function(){
    var arr = ['banana', 'monkey banana', 'apple', 'kiwi', 'orange'];

    var prohibited = ['banana', 'apple'];

 $.each(arr,function(arr_index,arr_value)
 {
   $.each(prohibited,function(p_index,p_value)
   {
      if(arr_value ==  p_value)
      {
          alert(arr_value+" = "+p_value);
      }
      else
      {
          alert(arr_value+" != "+p_value);
      }
   });
});

});
</script>
share|improve this answer
1  
I don't see jQuery anywhere mentioned in the question by OP, also, some explanation of "how your code works" will be helpful. – Mr. Alien May 25 at 10:31
1  
JQuery was not mentioned but the questioner requested to define some easier way so that he could understand easily. The code works good, because I have tested it. each function is similar to foreach, so nested each would solve the problem – Vijay Wilson May 25 at 10:55
    
Sure this will work, no doubt, but we shouldn't provide jQuery answers to JavaScript questions. – Mr. Alien May 25 at 11:08
    
I posted the answer with the intention that jQuery is javascript framework – Vijay Wilson May 25 at 11:14

For example by building a RegExp and testing against that:

var arr = ['banana', 'monkey banana', 'apple', 'kiwi', 'orange'];
var prohibited = ['banana', 'apple'];

var escapeForRegex = function(str) {
  return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
};

var r = new RegExp(prohibited.map(escapeForRegex).join("|"));
arr.filter(function(v){ return !r.test(v) });
share|improve this answer

I think this can be greatly simplified. There is already a built in within javascript for checking these things. Consider:

var arr = ['banana', 'monkey banana', 'apple', 'kiwi', 'orange'];

function checker(value) {
  var prohibited = ['banana', 'apple'];

  // indexOf() returns -1 if the element doesn't exist in the prohibited array
  return prohibited.indexOf( value ) == -1;
}

arr = arr.filter(checker);
console.log(arr);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.