Check for Partial Match in an Array

后端 未结 5 1251
长情又很酷
长情又很酷 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:08

    We need two loops to achieve this: something like below:

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

提交回复
热议问题