Javascript Array find(searchStr)

Description

Javascript Array find(searchStr)


Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i<this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);/*from www . j a  v  a 2s  .  c  om*/
      }
    } else {
      if (this[i]===searchStr) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    }
  }
  return returnArray;
}

Javascript Array find(searchStr)

Array.prototype.find = function (searchStr) {
    var returnArray = false;
    for (i = 0; i < this.length; i++) {
        if (searchStr instanceof RegExp) {
            if (searchStr.test(this[i])) {
                if (!returnArray) {
                    returnArray = []//from  w w  w. ja v a 2  s .  c  o m
                }
                returnArray.push(i);
            }
        } else {
            if (this[i] === searchStr) {
                if (!returnArray) {
                    returnArray = []
                }
                returnArray.push(i);
            }
        }
    }
    return returnArray;
};



PreviousNext

Related