There is a way to lower the sensibility of the indexOf? Like this
var arr = ["Hello", "stackoverflow"]
var idx = arr.indexOf("stack")
And get idx = 1 instead of idx = -1
There is a way to lower the sensibility of the indexOf? Like this
var arr = ["Hello", "stackoverflow"]
var idx = arr.indexOf("stack")
And get idx = 1 instead of idx = -1
You'd have to write your own function for that. Something like this would work:
function searchArr(arr, str) {
for (var i=0;i<arr.length;i++) {
if (arr[i].indexOf(str) !== -1) return i;
}
return -1;
}
searchArr(arr, 'stack');// returns 1
That simply iterates through the array you enter as the first argument, and keeps going until it finds an index where the array's value at that index contains the search string.
The following will return an array of true and false showing which array elements match "stack".
var arr = ["Hello", "stackoverflow"];
var idx =arr.map(function(s) {return s.indexOf("stack") !== -1});
If simply want to check if a match exists, you could use
var arr = ["Hello", "stackoverflow"];
var matchFound =arr.some(function(s) {return s.indexOf("stack") !== -1});