Home >Web Front-end >JS Tutorial >A simple way to add whether Array contains a string using JS

A simple way to add whether Array contains a string using JS

高洛峰
高洛峰Original
2016-12-08 14:03:471294browse

The Array type of JS does not have a method to determine whether it contains a certain string. The following code implements this function. Students in need can take a look:

Array.prototype.contains = function(v){
  var b = false;
  for(var i=0;i<this.length;i++){
    if(this[i] == v){
      b = true;
      break;
    }
  }
  return b;
};


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn