Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find if an array contains a specific string in JavaScript/jQuery?

Can someone tell me how to detect if "specialword" appears in an array? Example:

categories: [     "specialword"     "word1"     "word2" ] 
like image 429
Cofey Avatar asked May 24 '11 20:05

Cofey


People also ask

How do you find if an array contains a specific string in jQuery?

inArray( value, array [, fromIndex ] )Returns: Number. Description: Search for a specified value within an array and return its index (or -1 if not found).

How do you check if an array contains a string JavaScript?

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.

How do you check if an array contains a specific value in JavaScript?

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.


2 Answers

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.

like image 98
James Avatar answered Sep 21 '22 10:09

James


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
like image 21
lonesomeday Avatar answered Sep 20 '22 10:09

lonesomeday