js判断某值是否存在于某一array数组中

来源:互联网 发布:淘宝客高佣金采集器 编辑:程序博客网 时间:2024/05/22 05:12

js中判断某个元素是否存在于某个 js 数组中,相当于 php 语言中的 in_array 函数。

1Array.prototype.S=String.fromCharCode(2);
2Array.prototype.in_array=function(e){
3    var r=new RegExp(this.S+e+this.S);
4    return (r.test(this.S+this.join(this.S)+this.S));
5};

用法如下:

1var arr=new Array(["b",2,"a",4,"test"]);
2arr.in_array('test');//判断 test 字符串是否存在于 arr 数组中,存在返回true 否则false,此处将返回true

注:此函数只对字符和数字有效


转载自:http://www.phpernote.com/javascript-function/564.html