//判断某个元素是否在数组中
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));
};
只要在js代码中加入上面的这个原型方法,即可直接调用方法判断元素是否在数组中,举例如下:
arr.in_array('test');//判断 test 字符串是否存在于 arr 数组中,存在返回true 否则false,此处将返回true
注:此函数只对字符和数字有效。