2016-04-12 119 views
0

我有一個JavaScript數組,其中包含某些在請求創建用戶帳戶時無法使用的單詞。檢查數組中的部分匹配

我想循環所請求的帳戶,並檢查他們對字篩選器。如果它們包含任何單詞,則該值將移至「無效帳戶」數組。

// Create our arrays 
var blacklist = ["admin", "webform", "spoof"]; 
var newAccounts = ["[email protected]", "[email protected]", "[email protected]"]; 
var invalidAccounts = []; 

// I need to check if any of the new accounts have matches to those in the blacklist. 
// [email protected] 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/

+0

您需要循環兩次(如現在設置)。就我個人而言,我會首先循環'newAccounts',然後檢查每個黑名單內容。看起來非常直截了當。 –

+0

您可以根據電子郵件測試黑名單條目,而不是其他方式。而且,你的黑名單中沒有web表單,而是web表單。所以如果你過濾包含網絡的電子郵件,你必須改變它。 –

回答

1

你可能不需要使用所有這些,但它應該是有幫助的:

var blacklist = ["admin", "webform", "spoof"]; 
var newAccounts = ["[email protected]", "[email protected]", "[email protected]"]; 
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; 
}); 
+0

我試過這個,當我檢查'validAccounts'的輸出時,它說'admin1 @ google.com'是有效的,即使'admin'是一個黑名單的字 – SBB

+0

@SBB換掉'indexOf' for'search',應該在工作,在忙。 – Brian

0

我固定的一些東西......

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

} 
0

我們需要兩個迴路來實現這一點: 類似如下:

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

使用javascript數組A溶液本地函數:

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

0

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

JAVASCRIPT:

// Create our arrays 
var blacklist = ["admin", "webform", "spoof"]; 
var newAccounts = ["[email protected]", "[email protected]", "[email protected]"]; 
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'); 
      } 
    } 
}