Steve Gon
Steve Gon

Reputation: 462

Find a string in an Array

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;
}

Upvotes: 1

Views: 255

Answers (2)

NoobishPro
NoobishPro

Reputation: 2549

WORKING JSFIDDLE

It's because your recursion attempt was all weird. Most of your code was pretty decent. You also forgot 1 toLowerCase().

This should work;

var arr = ["Partner1", "Partner2"]
var needle = "partner1"
var n = findInArray(needle, arr, true, false);
console.log(n);

function findInArray(needle, arr, exact, sensitive) {
  //Check if these attributes were even given
  if (typeof needle != 'undefined' && typeof arr != 'undefined') {
    if (arr.length < 1) {
      return -1;
    }
    if (typeof exact == 'undefined') {
      //Also making sure it's always set. Defaults to false.
      exact = false;
    }
    if (sensitive == 'undefined') {
      //Also making sure it's always set. Defaults to false.
      sensitive = false;
    }
    //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);
      }

      //You got this quite right. Missed a toLowerCase on the last one.
      if (exact && sensitive && (haystack === needle)) {
        return i;
      } else if (exact && !(sensitive) && (haystack.toLowerCase() == needle.toLowerCase())) {
        return i;
      } else if (!(exact) && sensitive && (haystack.search(needle)) > -1) {
        return i;
      } else if (!(exact) && !(sensitive) && haystack.toLowerCase().search(needle.toLowerCase()) > -1) {
        return i;
      }
    }
  }
  return -1;
}

A few tiny optimisations

I also took the time to do a little optimisation for your code. JSFIDDLE

var arr = ["Partner1", "Partner2"]
var needle = "partner2"
var n = findInArray(needle, arr, true, false);
console.log(n);

function findInArray(needle, arr, exact, sensitive) {
  //Check if these attributes were even given
  if (typeof needle != 'undefined' && typeof arr != 'undefined') {
    if (arr.length < 1) {
      return -1;
    }
    if (typeof exact == 'undefined') {
      //Also making sure it's always set. Defaults to false.
      exact = false;
    }
    if (sensitive == 'undefined') {
      //Also making sure it's always set. Defaults to false.
      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;
}

Upvotes: 2

Zohaib Ijaz
Zohaib Ijaz

Reputation: 22875

You can use builtin Array.prototype.find or Array.prototype.indexOf

var arr = ["Partner1", "Partner2"];
var needle = "partner1";
var n = findInArray(needle, arr, true, false);
console.log(n);
function findInArray (input, array, exact, caseSenstive) {
   if (caseSenstive) {
      input = input.toLowerCase();
   }
   if (!exact){
      return array.find(i => i.toLowerCase().indexOf(input)) || -1;
   }
   if (caseSenstive) {
      return array.find(i => i === input) || -1;
   }
   return array.find(i => i.toLowerCase() === input) || -1;
}

Upvotes: 0

Related Questions