1.两个数组间互相校验
Var inArray = function(arr, item) {
for(var i = 0; i < arr.length; i++) {
if(arr[i] == item) {
return true;
}
}
return false;
};
var links = new Array();
links[0] = "notice";
links[1] = "purchase";
links[2] = "report";
var arr = new Array();
linkValue = "notice,def,ghi,jkl";
arr = linkValue.split(",");
for(i = 0; i < arr.length; i++) {
if(inArray(links, arr[i])) {
alert("存在" + arr[i]); //进行相关操作
} else {
alert("不存在" + arr[i]); //进行相关操作
}
}
2.某个字符串在数组中检验
给系统对象加一个扩展函数:
Array.prototype.contains = function (obj) {
var index = this.length;
while (index–) {
if (this[index] === obj) {
return true;
}
}
return false;
}
定义一个数组:
var arr_qf = [“jpg”, “png”, “gif”, “bmp”, “html” ,”jpeg”];
调用函数,检测是否存在:
arr_qf.contains(‘html’)