This question already has answers here :
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.
JavaScript Array includes() Method, JavaScript Array includes() Method The includes() method determines whether an array contains a specified element. This method returns true if the array contains the element, and false if not. The includes () method determines whether an array contains a specified element. This method returns true if the array contains the element, and false if not. Note: The includes () method is case sensitive.
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>
Check if value exists in Array - jQuery and JavaScript, indexOf() It is a JavaScript method that returns the index position of the value. If it doesn't find the value then it returns -1. It works both with string and an Array. The best option for this in modern Javascript is Array.prototype.includes: var found = categories.includes('specialword'); No comparisons and no confusing -1 results.
Here you go:
$.inArray('specialword', arr)
This function returns a positive integer (the array index of the given value), or -1
if the given value was not found in the array.
Live demo: http://jsfiddle.net/simevidas/5Gdfc/
You probably want to use this like so:
if ( $.inArray('specialword', arr) > -1 ) {
// the value is in the array
}
jQuery.inArray(), Description: Search for a specified value within an array and return its index (or - 1 if not found). inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when it doesn't find a match. If the first The following will return -1 (not found) because a number is being searched in an array of strings:� 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.
You can use a for
loop:
var found = false;
for (var i = 0; i < categories.length && !found; i++) {
if (categories[i] === "specialword") {
found = true;
break;
}
}
JavaScript Includes To Search In Strings & Arrays, The JavaScript includes method finds the target in an Array or String. Learn how to search arrays and strings to determine if they� In this case, we will use the includes () method which determines whether a string contains the specified word or a substring. If the word or substring is present in the given string, the includes () method returns true; otherwise, it returns false. The includes () method is case sensitive. This method does search the string.
I don't like $.inArray(..)
, it's the kind of ugly, jQuery-ish solution that most sane people wouldn't tolerate. Here's a snippet which adds a simple contains(str)
method to your arsenal:
$.fn.contains = function (target) {
var result = null;
$(this).each(function (index, item) {
if (item === target) {
result = item;
}
});
return result ? result : false;
}
Similarly, you could wrap $.inArray
in an extension:
$.fn.contains = function (target) {
return ($.inArray(target, this) > -1);
}
How to check if an array contains a specific string in jQuery?, Sometimes we need to check if a particular string exists in an array or not, we can do <script src="https://code.jquery.com/jquery-1.10.2.js"></script>; </head>� jQuery: $.inArray(value, array, types but if you want to find out if an array contains an object an array contains a specific string in JavaScript/jQuery?
JavaScript array: Find if an array contains a specific element , JavaScript: Find if an array contains a specific element Write a JavaScript function to find an array contains a specific element. ES6 Version: function contains(arr, element) { for (let i = 0; i < arr. length; i++) { if (arr[i] === element) { return true; } } return false; } arr = [2, 5, 9, 6]; console. In javascript the includes () method can be used to determines whether a string contains particular word (or characters at specified position).
How to check if an array includes an object in JavaScript , There are various methods to check an array includes an object or not. Using includes() Method: If array contains an object/element can be� The indexOf () method searches the array for the specified item, and returns its position. The search will start at the specified position, or at the beginning if no start position is specified, and end the search at the end of the array. Returns -1 if the item is not found.
jquery check if array of objects contains value Code Example, Get code examples like "jquery check if array of objects contains value" instantly right from your google search javascript by Victor Grk on Mar 26 2020 Donate. This array method helps us to find out the item in the array in JavaScript. If element exists in the array it returns the index position of the value and if the value doesn’t exist then it returns -1. It works with both string and an array in JavaScript.
Comments In pure JS: stackoverflow.com/a/25765186/1320932 pure JS : categories.includes("specialword") @patz watch out for pure JS, not supported in IE (any version) link @foxontherock start using transpiler - stop worrying about anything fact-checking, can-I-use-this-property kinda thing. James, that page does say it will work in IE9, as I indicated. Did you get it work for IE < 9 ? I believe I've run into this feature as missing in IE7 and IE8, but did not actually test; instead I relied on developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… indexOf is available in all major browsers, except IE < 9 what about 'foo' in arr
? @SuperUberDuper: That checks for if an object key exists: 1 in ['a'] -> false
1 in ['a', 'b'] -> true
'length' in [] -> true
It just so happens in JS an array is essentially an object with numeric keys. needsmorejquery.com How would I pass in the categories array inside inArray()? (this is data from a JSON feed) Here's an example of how I get links, which works (but it's not an array): var link = post.permalink;. Using post.categories returns this error in console.log: "can't convert null to object". @Cofey I don't know without more code. Parsing your JSON is hard without seeing it, and it's not really this question...