I currently have an if statement like this:
if (x==a||x==b||x==c||x==d||x==e) {
alert('Hello World!')
};
How can I instead test if x
equals any value in an array such as [a,b,c,d,e]
?
Thank you!
You can use
if([a,b,c,d,e].includes(x)) {
// ...
}
or
if([a,b,c,d,e].indexOf(x) !== -1) {
// ...
}
You can use the following code:
<script>
var arr = [ 4, "Pete", 8, "John" ];
var $spans = $("span");
$spans.eq(0).text(jQuery.inArray("John", arr));
$spans.eq(1).text(jQuery.inArray(4, arr));
$spans.eq(2).text(jQuery.inArray("Karl", arr));
$spans.eq(3).text(jQuery.inArray("Pete", arr, 2));
</script>
Read this link for more information about it
Hope this helps you.
check out jquery function inArray(): http://api.jquery.com/jQuery.inArray/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With