Check for Partial Match in an Array

后端 未结 5 1349
长情又很酷
长情又很酷 2021-01-26 03:23

I have a JavaScript array that contains some words that cannot be used when requesting user accounts to be created.

I am trying to loop over the accounts requested and c

5条回答
  •  情书的邮戳
    2021-01-26 04:06

    A solution using javascript array native functions:

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

    More info on: Array.filter and Array.some

提交回复
热议问题