如何从JavaScript中的数组检查电子邮件重复?

时间:2018-09-20 18:46:36

标签: javascript ecmascript-6 ecmascript-2017

有两封电子邮件,并尝试检查它是否与我的数据数组(即电子邮件数组)中的任何内容匹配。

以下内容将返回truefalse,但似乎不起作用:

 const email1 = test.emailAddress.value;
 const email2 = test2.emailAddress.value;

 data.every(({ email }) => email.value === email1 || email.value === email2);

3 个答案:

答案 0 :(得分:0)

您可以使用匹配的两封电子邮件数组filter const data = [ {email: 'test@test.com'}, {email: 'test1@test.com'}, {email: 'test2@test.com'} ] const email1 = 'test1@test.com' const email2 = 'test2@test.com' const matched = data.filter(({ email }) => [email1, email2].includes(email)); console.log(matched)

function multiplyAll(arr){
  var product =1;
  for (var i = 0; i < arr.length; i++){
    for(var j=0; j < arr[i].length; j++)
      product = product * arr[i][j];
  }
  //why is the product undefined?
return product;
}
//modify values below to test code
multiplyAll([[60,60],[24,365]]);

//console.log("Print results of product" + product);

答案 1 :(得分:0)

根据您的代码,.every方法检查email数组中的每个dataemail1还是email2,如果至少有一个data数组中的电子邮件无法满足条件,.every方法将返回false。这意味着.every方法不适合您要实现的目标。

var exists = data.some(({email}) => [email1, email2].includes(email))

假设data是包含电子邮件属性的对象数组。如果数据只是电子邮件字符串的数组,则应为

var exists = data.some(email => [email1, email2].includes(email))

答案 2 :(得分:-2)

除非您提供SELECT -- LOGINS COALESCE(sum( CASE WHEN speedy.base_transaction_type IN ('logins') THEN speedy.count ELSE 0 END),0) as login, -- REGISTRATIONS COALESCE(sum( CASE WHEN speedy.base_transaction_type IN ('registrations') THEN speedy.count ELSE 0 END),0) as registration, -- COMPLETED DEPOSITS COALESCE(sum( CASE WHEN speedy.base_transaction_type IN ('manual_deposit', 'deposit') AND LOWER(speedy.status) IN ('success', 'pending approval') THEN (speedy.amount_1 * (CASE WHEN in_currency = -1 THEN (SELECT getcurrencyexchangerate(speedy.currency_code)) ELSE 1 END) ) ELSE 0 END),0) as deposit, -- COMPLETED WITHDRAWS COALESCE(sum( CASE WHEN speedy.base_transaction_type IN ('withdraw') AND LOWER(speedy.status) IN ('success') THEN (speedy.amount_2 * (CASE WHEN in_currency = -1 THEN (SELECT getcurrencyexchangerate(speedy.currency_code)) ELSE 1 END) ) ELSE 0 END),0) as withdraw, -- BONUS SUM COALESCE(sum( CASE WHEN speedy.base_transaction_type IN ('bonus', 'manual_bonus') AND LOWER(speedy.status) IN ('success') THEN (speedy.amount_1 * (CASE WHEN in_currency = -1 THEN (SELECT getcurrencyexchangerate(speedy.currency_code)) ELSE 1 END) ) ELSE 0 END),0) as bonus, -- MANUAL BONUS SUM COALESCE(sum( CASE WHEN speedy.base_transaction_type IN ('manual_bonus') AND LOWER(speedy.status) IN ('success') THEN (speedy.amount_1 * (CASE WHEN in_currency = -1 THEN (SELECT getcurrencyexchangerate(speedy.currency_code)) ELSE 1 END) ) ELSE 0 END),0) as manualbonus, -- BONUS WAGERED COALESCE(sum( CASE WHEN speedy.base_transaction_type IN ('bonus_wagered') AND LOWER(speedy.status) IN ('success') THEN (speedy.amount_1 * (CASE WHEN in_currency = -1 THEN (SELECT getcurrencyexchangerate(speedy.currency_code)) ELSE 1 END) ) ELSE 0 END),0) as bonuswager FROM speedy_reports_data AS speedy WHERE speedy.for_date BETWEEN '2017-08-31' AND '2018-08-31' AND (CASE WHEN in_currency != -1 THEN speedy.currency_code = in_currency ELSE TRUE END) ,否则很难弄清楚发生了什么。但是您可以使用data检查数组中是否存在元素。

  

includes()方法确定数组是否包含某个元素,并在适当时返回true或false。

includes