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
本文介绍了一种在 JavaScript 中实现类似 PHP 的 in_array 功能的方法,通过自定义 Array.prototype.in_array 函数来检查某个元素是否存在于数组中,并提供了一个使用示例。
2191

被折叠的 条评论
为什么被折叠?



