0

This indexOf doesnt work anymore nowadays. There is method valueOf but seems to be doing something different. Im trying to do something like that :

var emails = [];
emails.push("sample@email");
if (emails.indexOf("sample@email") > 0) { //if this mail exists in array
//do something
}

Doesnt work this way.

Michal K
  • 49
  • 2
  • 9
  • 2
    Indexes start at 0. Instead, use `emails.indexOf("sample@gmail") > -1`. -1 is returned if the value isn’t in the array. – Jed Fox Nov 02 '16 at 13:00
  • 1
    Possible duplicate of [How do I check if an array includes an object in JavaScript?](http://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript) – Jed Fox Nov 02 '16 at 13:08

1 Answers1

0

I'm not entirely sure I'm answering this correctly.

I'm assuming your array isn't going to just contain one element, so lets assume you want to return 2 emails out of 3.

var array = [];
array.push("example@email.com");
array.push("example2@email.com");
array.push("example3@email.com");


console.log(array.includes("example@email.com", "example2@email.com")); //returns true

This is a very simple way of returning whether a value is in an array using the 'includes' method on the array object.

Reference: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/includes