I have 3 arrays and I need to find the array where the string matches.
Eg:
var arr1 = ['apple','banana','orange'];
var arr2 = ['abc','def','ghi'];
var arr3 = ['james','joe','robert','msg'];
var search = "abc joe 12345 msg:(somecontent)";
var arr = search.split(' ');
for(var i in arr){
if(arr1.indexOf(arr[i]) != -1) {
console.log('Found in arr1');
}
else if(arr2.indexOf(arr[i])!= -1){
console.log('Found in arr2');
}
else if(arr3.indexOf(arr[i])!=-1){
console.log('Found in arr3');
}
else{
console.log('Not Found');
}
}
Is there an efficient way of doing this search when there are thousands of items in each array.
One more trouble I had with the search string is that, I need msg to be split out from the (somecontent). One thing I can do is to split that arr[3].split(":");
. But the problem is msg:(somecontent) does not appear at the same position every time. So I cannot do the arr[3].split(":");
. I'm searching only in 'arr'. If I do the arr[3].split(":");
, I will create a new array and I won't be searching in that. I don't think I can avoid the second array.