I am trying to create a function that can be used in a variety of use cases for finding a value in a list (array). Here is the code I have:
function findInArray(needle, arr, exact, sensitive) {
if (needle && arr) {
var hayLength = arr.length
for (var i = 0; i < hayLength; i++) {
if (arr[0].length >= 0) {var haystack = arr[i][0];}
else {haystack = arr[i];}
if (exact && sensitive && (haystack === needle)) {return i;}
else if (exact && !(sensitive) && (haystack == needle)) {return i;}
else if (!(exact) && sensitive && (haystack.toLowerCase().search(needle.toLowerCase()))>-1) {return i;}
else if (!(exact) && !(sensitive) && haystack.search(needle)>-1) {return i;}
}
}
return -1;
}
I am sure the above code can be optimized, but I am not getting the third case to work when I want to match a string in a list ignoring case. E.g.
var arr = ["Partner1", "Partner2"]
var needle = "partner1"
var n = findInArray(needle, arr, true, false);
Returns -1.
I want the function to work with a 1D list, or multidimensional list, as well as find substrings (e.g. match "Google" and "Googler").
Answered: Combining @NoobishPro and @tehhowch "best of", this works well:
function findInArray(needle, arr, exact, sensitive) {
exact = exact !== false;
sensitive = sensitive !== false;
//We will catch the sensitivity parameter here to save performance
if (!sensitive) {
needle = needle.toLowerCase();
}
//determine array length
var hayLength = arr.length;
for (var i = 0; i < hayLength; i++) {
//Set haystack
var haystack = arr[i];
//Check if it's another array. If so, redo this function to go 1 level deeper.
if (haystack.constructor == Array) {
return findInArray(needle, haystack, exact, sensitive);
}
//We can lowercase it here to save on if-statement lowercasing
if (!sensitive) {
haystack = haystack.toLowerCase();
}
//easy one
if (exact && sensitive && (haystack == needle)) {
return i;
} else if (exact & (haystack == needle)) {
return i;
} else if (!exact & (haystack.search(needle)) > -1) {
return i;
}
}
return -1;
}