Can someone tell me how to detect if "specialword"
appears in an array? Example:
categories: [ "specialword" "word1" "word2" ]
inArray( value, array [, fromIndex ] )Returns: Number. Description: Search for a specified value within an array and return its index (or -1 if not found).
To check if a string is contained in an array, call the indexOf method, passing it the string as a parameter. The indexOf method returns the index of the first occurrence of the string in the array, or -1 if the string is not contained in the array.
JavaScript Array includes()The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found. The includes() method is case sensitive.
You really don't need jQuery for this.
var myarr = ["I", "like", "turtles"]; var arraycontainsturtles = (myarr.indexOf("turtles") > -1);
Hint: indexOf returns a number, representing the position where the specified searchvalue occurs for the first time, or -1 if it never occurs
or
function arrayContains(needle, arrhaystack) { return (arrhaystack.indexOf(needle) > -1); }
It's worth noting that array.indexOf(..)
is not supported in IE < 9, but jQuery's indexOf(...)
function will work even for those older versions.
jQuery offers $.inArray
:
Note that inArray returns the index of the element found, so 0
indicates the element is the first in the array. -1
indicates the element was not found.
var categoriesPresent = ['word', 'word', 'specialword', 'word']; var categoriesNotPresent = ['word', 'word', 'word']; var foundPresent = $.inArray('specialword', categoriesPresent) > -1; var foundNotPresent = $.inArray('specialword', categoriesNotPresent) > -1; console.log(foundPresent, foundNotPresent); // true false
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Edit 3.5 years later
$.inArray
is effectively a wrapper for Array.prototype.indexOf
in browsers that support it (almost all of them these days), while providing a shim in those that don't. It is essentially equivalent to adding a shim to Array.prototype
, which is a more idiomatic/JSish way of doing things. MDN provides such code. These days I would take this option, rather than using the jQuery wrapper.
var categoriesPresent = ['word', 'word', 'specialword', 'word']; var categoriesNotPresent = ['word', 'word', 'word']; var foundPresent = categoriesPresent.indexOf('specialword') > -1; var foundNotPresent = categoriesNotPresent.indexOf('specialword') > -1; console.log(foundPresent, foundNotPresent); // true false
Edit another 3 years later
Gosh, 6.5 years?!
The best option for this in modern Javascript is Array.prototype.includes
:
var found = categories.includes('specialword');
No comparisons and no confusing -1
results. It does what we want: it returns true
or false
. For older browsers it's polyfillable using the code at MDN.
var categoriesPresent = ['word', 'word', 'specialword', 'word']; var categoriesNotPresent = ['word', 'word', 'word']; var foundPresent = categoriesPresent.includes('specialword'); var foundNotPresent = categoriesNotPresent.includes('specialword'); console.log(foundPresent, foundNotPresent); // true false
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