Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

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

categories: [
    "specialword"
    "word1"
    "word2"
]
share|improve this question

marked as duplicate by Bergi, lonesomeday, Omar, madth3, Marc Audet Jul 3 at 0:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5 Answers

up vote 35 down vote accepted

jQuery offers $.inArray:

var found = $.inArray('specialword', categories) > -1;

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.

Example.

The pure Javascript option requires looping through the elements of the array manually, at least as a backup.

share|improve this answer
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 May 24 '11 at 20:41
@Cofey I don't know without more code. Parsing your JSON is hard without seeing it, and it's not really this question... – lonesomeday May 24 '11 at 20:44
Some browsers also support .indexOf: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… (not sure about IE). – Felix Kling May 24 '11 at 20:53
@Felix Indeed, hence "at least as a backup". Of course, jQuery uses the native indexOf if it exists. – lonesomeday May 24 '11 at 20:58
I've created a new post with my question in the comment above. That would be great if you could take a look at it! stackoverflow.com/questions/6117008/… – Cofey May 24 '11 at 21:26
show 2 more comments

You really don't need jQuery for this!

var myarr = ["I", "like", "turtles"];
var arraycontainsturtles = (myarr.indexOf("turtles") > -1);

or

function arrayContains(needle, arrhaystack)
{
    return (arrhaystack.indexOf(needle) > -1);
}

It's worth noting that jQuery's indexOf(...) function will work in IE < 9; .indexOf(..) is not supported by old versions of IE. Huh.

share|improve this answer
indexOf is available in all major browses, except IE >= 9 – Michael Paulukonis Jun 19 at 17:45
I disagree ie.microsoft.com/testdrive/HTML5/ECMAScript5Array/Default.html and I've just tested it. – James Jun 19 at 18:18
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/… – Michael Paulukonis Jun 19 at 18:21
oh, I see what I did there. – Michael Paulukonis Jun 19 at 18:22
1  
Thanks for that, I've edited my answer. Just when I though IE was a little substandard :P. – James Jun 19 at 18:26
show 1 more comment

Just use a for loop

var found = false;
for (i = 0; i < categories.length && !found; i++) {
  if (categories[i] === "specialword") {
    found = true;
  }
}
share|improve this answer
I may be totally wrong on this, but wouldn't you want to declare i in the for loop? If you don't put "var" in front, it'll put it in the global context (I think...), which might not be what you want. – aendrew Jul 11 at 10:24

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
}
share|improve this answer

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);
}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.