0

Have two emails and trying to check if it matches anything in my data array which is an array of emails.

Thought the below would return true or false but it seems to be not working:

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

 data.every(({ email }) => email.value === email1 || email.value === email2);
NAVIN
  • 3,193
  • 4
  • 19
  • 32
eman
  • 253
  • 2
  • 12
  • 3
    sounds like you need to use [Array.some](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some); The `some()` method tests whether **at least one element** in the array passes the test implemented by the provided function – Sphinx Sep 20 '18 at 18:49
  • You want to check if *every* item in the array matches `email1` or `email2`? – VLAZ Sep 20 '18 at 18:50
  • 1
    Whats the structure of `data`? – Abdulfatai Sep 20 '18 at 18:50

3 Answers3

0

You can filter your data with the matched array of two emails like so:

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)
The Reason
  • 7,705
  • 4
  • 24
  • 42
  • The problem is the email one is part of that data so I need to exclude that. bascially email1 will be the current emai that I'm checking and it will be already dynamically added to the data array. So it always returns a value. How would I get around that. – eman Sep 20 '18 at 19:08
0

According to your code, the .every method checks if every email in the data array is either email1 or email2, if at least one of the emails in the data array fails the condition, the .every method returns false. Which means that the .every method is not suitable for what you want to achieve.

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

Thats with the assumption that data is a array of objects that contain email attribute. If data is simply an array of email strings, then it should be

var exists = data.some(email => [email1, email2].includes(email))
Abdulfatai
  • 314
  • 2
  • 8
  • 2
    Don't do `.filter(…).length > 0`. There's a much more efficient specific method for this. – Bergi Sep 20 '18 at 19:05
  • Please give reasons @Bergi. – Abdulfatai Sep 20 '18 at 19:05
  • @Bergi, I will really love to know about the efficient method. – Abdulfatai Sep 20 '18 at 19:08
  • It's `.some` - it works like `.every` but returns as soon as it finds at least one match. A `.filter` will go through THE ENTIRE array, then construct AN ENTIRE NEW array for you to only check if the result has at least one match (what `.some` does) and then the result is discarded. – VLAZ Sep 20 '18 at 19:12
-2

Unless you provide your data it is hard to figure out what is going on. But you can useincludes to check if an element is present in an array or not.

The includes() method determines whether an array includes a certain element, returning true or false as appropriate.

if( data.includes(email1) || data.includes(email2)){ // ||  && depending upon your needs
   //do something
}
bipen
  • 36,319
  • 9
  • 49
  • 62