【问题标题】:Check for Partial Match in an Array检查数组中的部分匹配
【发布时间】:2016-04-12 17:13:27
【问题描述】:

我有一个 JavaScript 数组,其中包含一些在请求创建用户帐户时无法使用的单词。

我正在尝试遍历请求的帐户并根据单词过滤器检查它们。如果它们包含任何单词,则该值将移动到“无效帐户”数组中。

// Create our arrays
var blacklist = ["admin", "webform", "spoof"];
var newAccounts = ["admin1@google.com", "interweb@google.com", "puppy@google.com"];
var invalidAccounts = [];

// I need to check if any of the new accounts have matches to those in the blacklist. 
// admin1@google.com would need to be pushed into the invalidAccounts array because it 
// contains the word admin. Although interweb contains the word web, it does not contain 
// the full word, webform, so should be ignored.

// Loop over the blacklist array
for(var x = 0; x < blacklist.length; x++){
  if(blacklist[x].indexOf(newAccounts) > -1){
    alert(blacklist[x] + " is in the blacklist array");
    // Push the newAccounts value into the invalidAccounts array since it contains
    // a blacklist word.
  } else {
    alert('no matches');
  }
}

我需要在上面的代码中进行哪些更改以使其与上述部分字符串匹配?

上述代码的小提琴:https://jsfiddle.net/1qwze81o/

【问题讨论】:

  • 你需要循环两次(因为现在已经设置好了)。就个人而言,我会先遍历newAccounts,然后根据blacklist 的每个内容检查每个内容。看起来很简单。
  • 您可以针对电子邮件测试黑名单条目,而不是其他方式。此外,您的黑名单中有网络表单,而不是网络。因此,如果您过滤包含网络的电子邮件,则必须对其进行更改。

标签: javascript jquery arrays


【解决方案1】:

您可能不需要使用所有这些,但它应该会有所帮助:

var blacklist = ["admin", "webform", "spoof"];
var newAccounts = ["admin1@google.com", "interweb@google.com", "puppy@google.com"];
var invalidAccounts = [];

// transform each email address to an object of the form:
// { email: string, valid: boolean }
var accountObjects = newAccounts.map(function (a) {
    return { email: a, valid: true };
});

// loop over each account
accountObjects.forEach(function (account) {
    // loop over the blacklisted terms
    blacklist.forEach(function (blacklisted) {
        // check to see if your account email address contains a black listed term
        // and set the valid property accordingly
        account.valid = account.email.search(blacklisted) === -1;
    });
});

// filter accountObjects, validAccounts will now contain an array of valid objects
var validAccounts = accountObjects.filter(function (a) {
    return a.valid;
});

// back to the original type of a string array
var validEmailAddresses = validAccounts.map(function (a) {
    return a.email;
});

【讨论】:

  • 我试过这个,当我检查validAccounts 的输出时,它说admin1@google.com 是有效的,即使admin 是一个列入黑名单的词
  • @SBB 将 indexOf 替换为 search,现在应该可以使用了。
【解决方案2】:

使用javascript数组原生函数的解决方案:

var invalidAccounts = newAccounts.filter(function(account){ // we need to filter accounts
  return blacklist.some(function(word){ // and return those that have any of the words in the text
      return account.indexOf(word) != -1
  })
});

更多信息:Array.filterArray.some

【讨论】:

    【解决方案3】:

    我们需要两个循环来实现这一点: 如下所示:

       // Loop over the blacklist array
      for(var j = 0; x < newAccounts.length; j++){
        for(var x = 0; x < blacklist.length; x++){
        if(newAccounts[j].indexOf(blacklist[x]) > -1){
            alert(blacklist[x] + " is in the blacklist array");
            // Push the newAccounts value into the invalidAccounts array since it contains a blacklist word.
          }else{
            alert('no matches');
          }
        }
        }
    

    【讨论】:

      【解决方案4】:

      我修复了一些东西......

      for (var x = 0; x < newAccounts.length; x++) // loop through new accounts
      {
          // for every account check if there is any match in blacklist array
          for (var y = 0; y < blacklist.length; y++) 
          {
            // if there is match do something 
            if (newAccounts[x].indexOf(blacklist[y]) > -1)
            {
                alert(newAccounts[x] + " is in the blacklist array");
                break;
            }
        }
      
      }
      

      【讨论】:

        【解决方案5】:

        这是小提琴: https://jsfiddle.net/swaprks/n1rkfuzh/

        JAVASCRIPT:

        // Create our arrays
        var blacklist = ["admin", "webform", "spoof"];
        var newAccounts = ["admin1@google.com", "interweb@google.com", "puppy@google.com"];
        var invalidAccounts = [];
        
        for(var x = 0; x < newAccounts.length; x++){
            for(var y = 0; y < blacklist.length; y++){
        
        
                  if( newAccounts[x].indexOf(blacklist[y]) > -1){
                    alert(blacklist[x] + " is in the blacklist array");
                    // Push the newAccounts value into the invalidAccounts array since it contains
                    // a blacklist word.
                    invalidAccounts.push(newAccounts[x]);
                  }else{
                    alert('no matches');
                  }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2013-08-22
          • 1970-01-01
          • 2015-12-08
          • 1970-01-01
          • 1970-01-01
          • 2020-12-22
          • 2023-03-27
          • 2018-06-04
          • 1970-01-01
          相关资源
          最近更新 更多