javascript模拟php函数in_array
作者:hebedich 时间:2023-10-15 22:12:49
js 中判断某个元素是否存在于某个 js 数组中,相当于 php 语言中的 in_array 函数。
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));
};
用法如下:
var arr=new Array(["b",2,"a",4,"test"]);
arr.in_array('test');//判断 test 字符串是否存在于 arr 数组中,存在返回true 否则false,此处将返回true
注:此函数只对字符和数字有效
jQuery中有类似的函数:http://docs.jquery.com/Utilities/jQuery.inArray
它的代码如下:
function inArray(needle, haystack) {
var length = haystack.length;
for(var i = 0; i < length; i++) {
if(haystack[i] == needle) return true;
}
return false;
}
标签:javascript,inarray
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python+requests接口自动化框架的实现
2022-11-25 04:03:40
![](https://img.aspxhome.com/file/2023/8/96958_0s.png)
vue.js2.0 实现better-scroll的滚动效果实例详解
2023-07-02 16:58:50
![](https://img.aspxhome.com/file/2023/4/139864_0s.png)
Python中OpenCV实现查找轮廓的实例
2023-12-05 05:26:24
![](https://img.aspxhome.com/file/2023/4/112414_0s.png)
Python中functools模块的常用函数解析
2022-08-12 08:10:50
Python人脸检测实战之疲劳检测
2021-02-23 16:31:37
![](https://img.aspxhome.com/file/2023/6/76716_0s.jpg)
SQL Server简单模式下误删除堆表记录恢复方法(绕过页眉校验)
2024-01-15 00:18:11
![](https://img.aspxhome.com/file/2023/8/129308_0s.png)
ThinkPHP模板范围判断输出In标签与Range标签用法详解
2024-05-05 09:17:00
php中strtotime函数性能分析
2023-11-22 13:21:05
详解Ubuntu16.04安装Python3.7及其pip3并切换为默认版本
2023-07-30 10:18:13
![](https://img.aspxhome.com/file/2023/0/92400_0s.png)
Docker安装常用组件(mysql,redis)的方法
2024-01-28 02:44:56
MSSQL 基本语法及实例操作语句
2012-07-11 15:40:09
window.onload使用指南
2024-04-18 10:58:51
Python 可迭代对象 iterable的具体使用
2021-08-28 08:24:32
goFrame的队列gqueue对比channel使用详解
2024-04-26 17:16:24
![](https://img.aspxhome.com/file/2023/1/134391_0s.jpg)
Python机器学习应用之基于决策树算法的分类预测篇
2021-10-28 20:53:14
![](https://img.aspxhome.com/file/2023/4/68414_0s.png)
一文详解PyQt5中信号(Signal)与槽(Slot)
2022-06-27 19:53:08
![](https://img.aspxhome.com/file/2023/8/65898_0s.gif)
一文读懂python Scrapy爬虫框架
2021-01-31 10:08:31
![](https://img.aspxhome.com/file/2023/0/131040_0s.jpg)
自学python求已知DNA模板的互补DNA序列
2022-07-05 13:24:56
![](https://img.aspxhome.com/file/2023/2/95402_0s.jpg)
详解Python如何获取视频文件的大小和时长
2021-12-08 14:50:13
![](https://img.aspxhome.com/file/2023/1/126921_0s.jpg)
python标识符命名规范原理解析
2023-05-28 04:06:42