1

I need to find an object (a string to be precise) inside a large array. While the below code works, it's tediously scrolling through each element of the array, a brute method. Is there a more efficient method? possibly calling on .search or .match or equivalent? Also how to make the search object (string) case insensitive? i.e object might be "abc" while array element is "ABC".

Many thanks in advance

function SearchArray(array, object){ //need to modify code to become case insensitive.
  for (var i= 1; i< array.length; i++){
    if (array[i] == object.toString()){ 
        return i; 
      } 
  }
  return 0;
}

I also forgot to mention that the search returns the index / position of the matched object within the one dimensional array, rather than simple true / false.

3
  • if the array isn't sorted, then theres no faster way to do this than iterating through each element. if it is sorted, a binary search is probably a good idea. for case insensitivity, you can call toLowerCase() on both the array element and the object string before comparing. Commented Jun 13, 2013 at 15:41
  • if you have many lookups to do it might be more efficient to put your data as attributes of an object, that way you can just check for membership using in instead of looping through the whole array Commented Jun 13, 2013 at 15:46
  • You should change the return 0 to return -1 in your example... 0 points at the first element of the array ;) Commented Jun 13, 2013 at 15:54

2 Answers 2

2

The following function does just that:

function findWord(array, word) {
    return -1 < array.map(function(item) { return item.toLowerCase(); }).indexOf(word.toLowerCase());
}

What it does is:

  • Converting every string to lower case using the map() function.
  • Searching for a certain word also in lower case mode.
  • If the word is found, the function returns a value greater than -1, therefore the return value is either true or false
2
  • Is there a way to get the function to return index of the matched object in the array instead of true or false? Commented Jun 13, 2013 at 16:10
  • 3
    Sure, just omit the -1 < . Commented Jun 14, 2013 at 11:19
1

If you are only going to search the large array once then the only likely optimization is to store the object string representation instead of generating it before each comparison:

function SearchArray(array, object) {
  var len=array.length, str=object.toString().toLowerCase();
  for (var i=0; i<len; i++) {
    if (array[i].toLowerCase() == str) { return i; }
  }
  return -1; // Return -1 per the "Array.indexOf()" method.
}

However, if you will be searching for many objects within the array then you will save time by storing the lower-case version of the elements:

var lowerArray = array.map(function(x){return x.toString().toLowerCase();});
var lowerObject = object.toString().toLowerCase();
lowerArray.indexOf(lowerObject); // Simply use "Array.indexOf()".

Moreover, if you will be searching this array many times, plenty of memory is available, and performance is critical then you should consider using an object for O(1) lookup:

function makeLowerCaseArrayIndexLookupFunction(array) {
  var lookup = array.reduce(function(memo, x, i) {
    memo[x.toString().toLowerCase()] = i;
    return memo;
  }, {});
  return function(obj) {
    var idx = lookup[obj.toString().toLowerCase()];
    return (typeof(idx)==='undefined') ? -1 : idx;
  }
}

var findWeekdays = makeLowerCaseArrayIndexLookupFunction([
  'Mon', 'Tues', 'Weds', 'Thurs', 'Fri', 'Sat', 'Sun'
]);
findWeekdays('mon'); // => 0
findWeekdays('FRI'); // => 4
findWeekdays('x'); // => -1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.