在数组中查找字符串

时间:2018-06-07 01:22:20

标签: javascript arrays multidimensional-array google-apps-script

我正在尝试创建一个可以在各种用例中使用的函数,用于在列表(数组)中查找值。这是我的代码:

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

我确信上面的代码可以进行优化,但是当我想在列表忽略大小写时匹配字符串时,我没有得到第三种情况。 E.g。

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

返回-1。

我希望该函数能够使用一维列表或多维列表,以及查找子字符串(例如匹配“Google”和“Google”)。

回答: 结合@NoobishPro和@tehhowch“最好的”,这很好用:

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

2 个答案:

答案 0 :(得分:2)

WORKING JSFIDDLE

这是因为你的递归尝试都很奇怪。你的大部分代码都相当不错。你也忘记了1 toLowerCase()

这应该有用;

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

一些微小的优化

我还花时间为您的代码做了一些优化。 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;
}

答案 1 :(得分:0)

您可以使用内置Array.prototype.findArray.prototype.indexOf

&#13;
&#13;
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;
}
&#13;
&#13;
&#13;