js中判断某个元素是否存在于某个 js 数组中,相当于 php 语言中的 in_array 函数。
1 | Array.prototype.S=String.fromCharCode(2); |
2 | Array.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 | }; |
用法如下:
1 | var arr= new Array([ "b" ,2, "a" ,4, "test" ]); |
2 | arr.in_array( 'test' ); //判断 test 字符串是否存在于 arr 数组中,存在返回true 否则false,此处将返回true |
注:此函数只对字符和数字有效
转载自:http://www.phpernote.com/javascript-function/564.html