JS function implementation method for determining whether an array contains a certain element
Array.prototype.contains = function(obj) { var i = this.length; while (i--) { if (this[i] === obj) { return true; } } return false;}or
Array.prototype.contains = function(element) { for (var i = 0; i < this.length; i++) { if (this[i] == element) { return true; } } return false;}or
Array.prototype.in_array = function(e) { for(i=0; i<this.length && this[i]!=e; i++); return !(i==this.length);}Another big bull wrote this:
Array.prototype.S = String.fromCharCode(2);Array.prototype.in_array = function(e) { var r = new RegExp(this.S+e+this.S); return (r.test(this.S+this.join(this.S)+this.S));}How to use it is:
var arr=["a","b"];
alert(arr.in_array("a"))
It is said that while iterating is the fastest method in JS. I don’t know if it is true.
http://stackoverflow.com/questions/237104/javascript-array-containsobj
The discussion here is very intense, it is recommended to take a look. If you use jQuery, you can directly use jQuery to implement it. Reference address:
http://api.jquery.com/jQuery.inArray/
The above article to determine whether an array contains a certain element's js function implementation method is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.