Element is present in an Array

Hello!
How can i do to find if element is present in an array in javascript?

There are two common methods of doing this:


arr.indexOf(x) >= 0

// As of ES2016, you can use includes()
arr.includes(x)

for example i have an array ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, how can i do verify if present element ‘a’?
you can show me an example whit this array? Thank you.

var arr = [ 'a', 'b', 'c', 'd', 'e' ];

if (arr.indexOf('a') >= 0) {
  console.log("Found 'a' in array");
}

Edit: indexOf() returns the array index that the given item is found at, or -1 if the item is not found.

Thanks for the answare, now I would verify the presence of two element in an array but not one at time, I would verify the presence of two element at the same time…you can do this? there is a method?

Thank you.

No, for multiple elements you’ll need to test individually.

I have an array whit 52 cards, I can show 5 cards at a time, and in this array of 5 cards I wolud define a couple of cards, double couple of cards, tris of cards, just like a poker…you can show me any methods please???

Give an example of an array of 5 cards and what you need to look for.
Are you saying you need to define a condition such as “contains a triple” and then test if the array of 5 contains three of the same cards? Something like that?

So then you would test the 5 cards against all possible poker hands?

for example this array is generated random: Array is ‘9c’,‘Aq’,‘Af’, ‘Jp’, ‘Kp’ ecc… the couple of cards could be ‘Aq’ ‘Af’, I can get value and index of cards but i can’t to associate…I try to various condition but i can’t, you can help me? Thanks.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.