检查字符串是否包含 JavaScript 中数组的任何元素 [英] Check if a string contains any element of an array in JavaScript

查看:34
本文介绍了检查字符串是否包含 JavaScript 中数组的任何元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查字符串是否包含数组的任何元素?如果元素有一些字符串,我想过滤一些数组.请看下面的代码.

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

结果是 [ 'apple', 'kiwi', 'orange' ].'apple' 应该被删除,但事实并非如此.

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?

推荐答案

问题出在 for 循环,从 return 结束函数开始只迭代一次,在此过程中切断了 for 循环.因此,您可以像这样更新代码,使函数仅在 for 循环完成后返回.

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

为了减少功能,您可以使用 every()indexOf() 方法

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

'every' 方法为数组中存在的每个元素执行一次提供的回调函数,直到它找到一个回调函数返回假值(转换为布尔值时变为假的值)的函数.如果找到这样的元素,every 方法会立即返回 false.否则,如果回调为所有元素返回真值,则每个元素都将返回真值.仅对已分配值的数组索引调用回调;它不会为已删除或从未分配值的索引调用.(取自这里)

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

对于旧浏览器 检查 polyfill 选项每种方法.

您甚至可以在这里使用正则表达式.使用数组生成正则表达式并使用 test() 检查匹配

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

参考这个字符串到正则表达式转换的答案:您可以使用字符串变量动态创建 JavaScript 正则表达式吗?

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

这篇关于检查字符串是否包含 JavaScript 中数组的任何元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆