5

I have an array of strings, I want to use jQuery to see if a particular string is contained within the array?

Can i do this with jQuery>?

CLiown
  • 13,665
  • 48
  • 124
  • 205

3 Answers3

4

You can use $.inArray() and check that the result is not == -1, like this:

var arr = [ "string1", "string2" ];
jQuery.inArray("string1", arr) // returns 0
jQuery.inArray("string2", arr) // returns 1
jQuery.inArray("string3", arr) // returns -1

And for the flame wars about "why use jquery?" here...it's because older IE (and maybe current IIRC) doesn't have the Array.indexOf function, $.indexOf() will use the built-in Array.indexOf is it's present, it's just a wrapper to take care of IE not having this.

Alternatively, you can add the Array.indexOf method if it's not present, bobince shows how to do that here.

Community
  • 1
  • 1
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
1
if( $.inArray("theString", theArray) > -1 ) {
    alert('found one');
}
user113716
  • 318,772
  • 63
  • 451
  • 440
0
var arr = ["foo", "bar"];
if(/\bfoo\b/.test(arr.join('|'))) alert('yay');

I didn't want to start the flamewars on Nick's answer :p But this plain javascript should do it aswell for OPs requirement.

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • I don't think this would always work. What if there's no "foo" in the array, but there is "food"? – user113716 Jun 08 '10 at 14:01
  • This is a very expensive approach to do something that's in native/optimized code everywhere except IE, I wouldn't go this route, seems like a lot of extra CPU cycles for a less accurate/usable result, the array method also works if it's a number for instance. – Nick Craver Jun 08 '10 at 14:08
  • @Nick: no doubt about that, it's more an.. educational answer of possibilitiys :) – jAndy Jun 08 '10 at 14:10
  • Seems closer, but what if there's no array entry "foo" but there is one with "foo bar"? Although, I'll admit that the OP isn't clear whether it is intended to match an entire array item, or a sub-string within an entry. If that's the case, then your answer is the *most correctest*. ;o) – user113716 Jun 08 '10 at 14:10
  • @Nick: a quick Firebug.time() benchmark result: `regex: 51ms`, `jQuery: 92ms` over 10.000 iterations. Just for the CPU cycles. – jAndy Jun 08 '10 at 14:16
  • @patrick - the reverse is true as well, for example try `"foor bar"`, it's true, even though it's not in the array, but is in the joined string it's checking. @jAndy - You're not comparing 2 equivalent functions, so the result doesn't matter...your versions gets invalid results :) – Nick Craver Jun 08 '10 at 14:18
  • Assuming the OP wants to match against the entire string in each entry, it may be safer to change your `join()` to something more obscure than a blank space, like `~|~`, or whatever. – user113716 Jun 08 '10 at 14:19
  • @Nick: Not if OP just wants to know if `foo` is part of an array string :) – jAndy Jun 08 '10 at 14:19
  • @jAndy - `var arr = ["foo", "bar"]; if(/\bfoo bar\b/.test(arr.join(' '))) alert('yay');` this should *not* be true. – Nick Craver Jun 08 '10 at 14:20
  • @Nick: true, I adopted the join as patrick suggested. I'm still horny over the fact that it beats jQuery in performance :p I'm not a regexp ninja, maybe someone can optimize that expression. – jAndy Jun 08 '10 at 14:21
  • @jAndy - Don't forget to change the regular expression as well to `/\|foo\|/`. Also, you'll need to add a `|` to the beginning and end of the string after it is joined. – user113716 Jun 08 '10 at 14:24
  • @jAndy: In Chrome I see this 71ms vs `.indexOf()` 49ms when the array has 6 elements, the jquery/`indexOf()` method takes 49ms...this totally depends on the JS engine as to which is more efficient, add more items and the result is more severe: http://jsfiddle.net/xDeQT/ With 200 items RegEx takes 1192ms, jQuery/`.indexOf()` stays at 48ms...try it in FireFox as well, you'll see similar scaling behavior. I'm not sure why the base `.indexOf()` performance is so different between Chrome/Firefox, but increase the numbers to see how poorly string operations scale, and still no position returned :) – Nick Craver Jun 08 '10 at 14:29
  • @Nick: yes you're right obv. Firefox has not that big difference, its 2045ms vs 5487ms. Well, regular expression `testing` beats jQuery only on small scales tho. – jAndy Jun 08 '10 at 14:33
  • @jAndy: Again, while it *may* be faster in *some* browsers *only for small numbers*, you're not returning the same information, your result doesn't let me use the index in the array, so you can't compare the two. I can say "wow IEs fast!"...but does it matter, since it didn't render my page correctly? – Nick Craver Jun 08 '10 at 14:37
  • @Nick: I agree, but read the OPs requirement again, he just wants to know if a string is containend in an array no matter what. That requirement is fulfilled IMO. – jAndy Jun 08 '10 at 14:39
  • @jAndy - `"foo|bar"`, not in the array, still `true`...in short you can't replicate the behavior, you can *hope* your delimiter isn't present, but that's not the same :) – Nick Craver Jun 08 '10 at 14:42
  • @Nick - Of course, technically the OP was asking to test `if`, not `where`. Still, I think `$.inArray()` is the way to go. It would be cool if `$.inArray` would accept a regular expression as the search string, so you could test for sub-strings. Or can it? – user113716 Jun 08 '10 at 14:45
  • @Nick - Looks like I need to refresh the page before commenting. :o) – user113716 Jun 08 '10 at 14:47