=========自己写的方法
Array.prototype.contains = function (element) {???
??????? for (var i = 0; i < this.length; i++) {
??????? ? var valueStr = this[i];
???????? ? if (valueStr[0] == element) {
?????????????? ?return true;???
??????? ? }
??}???
??return false;
}
?
var myArray = new Array(
new Array('山东','德州','济南'),
new Array('河南','南阳'));
??
alert(myArray.contains('山东'));
比较二位数组
?
?
?
?
JS中如何判断数组中是否包含某一元素
1.
Array.prototype.contains = function (element) {????
???????? for (var i = 0; i < this.length; i++) {????
???????????????if (this[i] == element) {????
?????????????? return true;????
??????? }????
}????
???????? return false;????
}??
2.
Array.prototype.contains = function(obj) {????
??????? var i = this.length;????
??????? while (i–) {????
????????????? if (this[i] === obj) {????
???????????????????? return true;????
????????????? }????
??????? }????
??????? return false;????
}
3.我是用这个实现的
json1.push(vipStatus);
Array.prototype.contains = function(item){
? return RegExp(item).test(this); }
if(json1.contains('无效')){
????? return ture;
}else{
????? return false
}
?
?
要判断数组中是否包含某个元素,从原理来来说,就是遍历整个数组,然后判断是否相等,我们来造个轮子,名字就山寨PHP的数组函数in_array()
?
- Array.prototype.in_array?=?function(e)??