i need to check if a string is in array as part of a string of any item in the array. Lets say one of the items in the array is "Hello World" and the string is "ello", I need that to return true.
Asked
Active
Viewed 264 times
1
-
take a look at this question http://stackoverflow.com/questions/3975871/optimize-search-through-large-js-string-array – Shaheer Feb 10 '12 at 06:02
4 Answers
2
Yeah, you could do all that. Or-
if(String(array).indexOf('ello')!=-1){
ello is in the array
}

kennebec
- 102,654
- 32
- 106
- 127
-
i did not know that this type of syntax is available out there. gr8 work man! – Shaheer Feb 11 '12 at 20:54
1
Just step through the array and check each string.
function isStringInArray(data, stringToFind) {
for (var i = 0; i < data.length; i++) {
if (data[i].indexOf(stringToFind) > -1) return true;
}
return false;
}

Rophuine
- 724
- 3
- 8
-
-
@Shaheer Is there a problem besides caching of the `length` of the array? – alex Feb 10 '12 at 06:05
-
the problem lies in the loop. it will loop through each element of the array – Shaheer Feb 10 '12 at 06:07
-
This loop should work fine for over a thousand elements. It should work fine for a billion elements - but it would be very slow. Are you expecting to deal with a very large volume of data? How large? – Rophuine Feb 10 '12 at 06:07
-
@Shaheer Only if the substring was never found. How else can it work? Binary search? – alex Feb 10 '12 at 06:08
-
@Shaheer - if your data volume isn't too enormous it should be fine. There are strategies for searching large volumes of data, but they involve keeping some metadata as you build your array. For example, you could store a dictionary of trigrams, with each entry containing pointers to every array element containing that trigram - then you would just be able to search each trigram in your string to search for, and check only the strings pointed to by those trigram lists. Let me know if this sounds useful and I'll flesh out some code. – Rophuine Feb 10 '12 at 06:11
-
1_"the problem lies in the loop. it will loop through each element of the array"_ - Why is that a problem? How can you check the value of each element without looping through each element? – nnnnnn Feb 10 '12 at 06:15
-
tried you code without success: http://jsbin.com/uciqax/edit#javascript,html,live – boruchsiper Feb 10 '12 at 06:19
-
i didn't say that it should not loop, i am just saying that the iterations should be minimum, there are already well developed searching algorithms for that -- provided that he actually have that large number of elements in the array, otherwise this solution should be fine. – Shaheer Feb 10 '12 at 06:21
-
-
@Shaheer: Just given an array of strings, I don't think there are well developed searching algorithms. If you know the array is sorted, it won't help - because you're after any substring. Is performance an issue? I can explain the trigram algorithm I mentioned earlier, but I'm not going to that effort only to find out you're only talking about 10,000 strings. This approach is fine for the sorts of data sizes you tend to find in javascript problems. – Rophuine Feb 10 '12 at 07:02
1
If you have a modern browser or want to polyfill Array.prototype.some()
(alternatively use a library such as Underscore).
var contains = ['A', 'Hello World'].some(function(member) {
return ~member.indexOf('ello');
});
You tagged this jQuery so you could also use this (will work in all browsers you probably care about)...
var contains = !!$.grep(['A', 'Hello World'], function(member) {
return ~member.indexOf('ello');
}).length;

alex
- 479,566
- 201
- 878
- 984
0
You could do something like this:
function contains(arr, str) {
var i = 0,
len = arr.length;
for (; i < len; ++i) {
if (arr[i].indexOf(str) > -1) {
return true;
}
}
return false;
}
Obviously it's naive in a few ways, for example it doesn't account for case sensitivity, nor does it tell you which or how many items matched, but you can easily modify it to do so.

Evan Trimboli
- 29,900
- 6
- 45
- 66
-
+1. Why on earth did somebody vote this down? It's (virtually) the same as another answer that was voted _up_ except that this one was posted first. (I don't really care for the style of initialising loop-control variables _before_ the `for` statement, but that's hardly a reason for a downvote.) – nnnnnn Feb 10 '12 at 06:20