2015-07-023lian8 的分享 加三联MM小编微信好友:sanlian2018
本文实例讲述了JavaScript判断数组是否包含指定元素的方法。分享给大家供大家参考。具体如下:
这段代码通过prototype定义了数组方法,这样就可以在任意数组调用contains方法
?
1
2
3
4
5
6
7
8
9
10
11
12
|
/**
* Array.prototype.[method name] allows you to define/overwrite an objects method
* needle is the item you are searching for
* this is a special variable that refers to "this" instance of an Array.
* returns true if needle is in the array, and false otherwise
*/
Array.prototype.contains = function ( needle ) {
for (i in this) {
if (this[i] == needle) return true;
}
return false;
}
|
用法:
?
1
2
3
4
5
|
// Now you can do things like:
var x = Array();
if (x.contains('foo')) {
// do something special
}
|
希望本文所述对大家的javascript程序设计有所帮助。
这篇文章主要介绍了简介可以自动完成UI的AngularJS工具angular-smarty,包括其中隔离作用域绑定指令符和promise的使用,需要的朋友可以参考下 我们最近为我 ...
实际上,setTimeout和setInterval的语法相同。它们都有两个参数,一个是将要执行的代码字符串,还有一个是以毫秒为单位的时间间隔,当过了那个时间段之后就 ...
这篇文章主要介绍了在JavaScript的AngularJS库中进行单元测试的方法,主要针对AngularJS中的控制器相关,需要的朋友可以参考下 开发者们都一致认为单元测试 ...