我有两个数组。一个包含电子邮件列表,另一个包含匹配时应拒绝的字符串列表。

array1 = [ '[email protected]',
  '[email protected]',
  '[email protected]',
  '[email protected]',
  '[email protected]',
  '[email protected]',
  '[email protected]',
  '[email protected]' ]

array 2 = [ /calendar-notification/i,
  /feedproxy/i,
  /techgig/i,
  /team/i,
  /blog/i,
  /info/i,
  /support/i,
  /admin/i,
  /hello/i,
  /no-reply/i,
  /noreply/i,
  /reply/i,
  /help/i,
  /mailer-daemon/i,
  /googlemail.com/i,
  /mail-noreply/i,
  /alert/i,
  /calendar-notification/i,
  /eBay/i,
  /flipkartletters/i,
  /pinterest/i,
  /dobambam.com/i,
  /notify/i,
  /offers/i,
  /iicicibank/i,
  /indiatimes/i,
  /[email protected]/i,
  /facebookmail/i,
  /message/i,
  /facebookmail.com/i,
  /notification/i,
  /youcanreply/i,
  /jobs/i,
  /news/i,
  /linkedin/i,
  /list/i ]

array2 包含我想拒绝的所有无效电子邮件。

我如何比较这两个数组并从 array1 中删除无效的电子邮件,以便我得到
array3 = [ '[email protected]',
  '[email protected]',
  '[email protected]',
  '[email protected]',
  '[email protected]',
  '[email protected]' ]

最佳答案

您基本上可以对它们进行 filter:

var newArray = array1.filter(function (elem) {
    var ok = true
    array2.forEach(function (tester) {
        if (tester.test(elem)) {
            ok = false;
        }
    });
    return ok
});

更新

正如@torazaburo 所建议的,使用 some 我们可以有一个更干净的解决方案:
var newArray = array1.filter(function (elem) {
    return !array2.some(function (tester) {
        return tester.test(elem)
    });
});

关于javascript - 比较两个数组并在正则表达式之后返回唯一值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35917447/

10-11 12:58