Home  >  Article  >  Web Front-end  >  How to determine whether a given variable value is contained in an array in javascript?

How to determine whether a given variable value is contained in an array in javascript?

伊谢尔伦
伊谢尔伦Original
2017-07-26 13:12:001703browse

In JS, there is no function that can be used directly to determine whether a value is in an array. For example, there is the in_array() function in PHP. But we can write a function similar to in_array() to determine whether a value is in the function

Example 1

/* 
* 
* 判断在数组中是否含有给定的一个变量值 
* 参数: 
* needle:需要查询的值 
* haystack:被查询的数组 
* 在haystack中查询needle是否存在,如果找到返回true,否则返回false。 
* 此函数只能对字符和数字有效 
* 
*/ 
function findnum(){ 
var a=[1,2];//假设a是数组,obj是要判断的数 
var obj=1; 
var b = false; 
for (var i = 0; i < a.length; i++) { 
if (a[i] == obj) { 
b = true;break; 
} 
} 
if (b) 
alert("数组中存在a[" + i + "]:" + a[i]); 
else 
alert("数组中不存在"+obj); 
}

Example 2

/** 
* JS判断一个值是否存在数组中 
* 琼台博客 
*/ 
// 定义一个判断函数 
var in_array = function(arr){ 
// 判断参数是不是数组 
var isArr = arr && console.log( 
typeof arr===&#39;object&#39; ? arr.constructor===Array ? arr.length ? arr.length===1 ? arr[0]:arr.join(&#39;,&#39;):&#39;an empty array&#39;: arr.constructor: typeof arr 
); 
// 不是数组则抛出异常 
if(!isArr){ 
throw "arguments is not Array"; 
} 
// 遍历是否在数组中 
for(var i=0,k=arr.length;i<k;i++){ 
if(this==arr[i]){ 
return true; 
} 
} 
// 如果不在数组中就会返回false 
return false; 
} 
// 给字符串添加原型 
String.prototype.in_array = in_array; 
// 给数字类型添加原型 
Number.prototype.in_array = in_array; 
// 声明一个数组 
var arr = Array(&#39;blue&#39;,&#39;red&#39;,&#39;110&#39;,&#39;120&#39;); 
// 字符串测试 
var str = &#39;red&#39;; 
var isInArray = str.in_array(arr); 
alert(isInArray); // true 
// 数字测试 
var num = 119; 
var isInArray = num.in_array(arr); 
alert(isInArray); // false 
如果传入的不是数组则会抛出异常 
/** 
* JS判断一个值是否存在数组中 
* 琼台博客 
*/ 
// 定义一个判断函数 
var in_array = function(arr){ 
// 判断参数是不是数组 
var isArr = arr && console.log( 
typeof arr===&#39;object&#39; ? arr.constructor===Array ? arr.length ? arr.length===1 ? arr[0]:arr.join(&#39;,&#39;):&#39;an empty array&#39;: arr.constructor: typeof arr 
); 
// 不是数组则抛出异常 
if(!isArr){ 
throw "arguments is not Array"; 
} 
// 遍历是否在数组中 
for(var i=0,k=arr.length;i<k;i++){ 
if(this==arr[i]){ 
return true; 
} 
} 
// 如果不在数组中就会返回false 
return false; 
} 
// 给字符串添加原型 
String.prototype.in_array = in_array; 
// 给数字类型添加原型 
Number.prototype.in_array = in_array; 
// 声明一个数组 
var arr = null; 
// 字符串测试 
var str = &#39;red&#39;; 
var isInArray = str.in_array(arr); 
alert(isInArray); // uncaught exception: arguments is not Array 
JS判断一个数组中是否有重复值的 
var ary = new Array("111","22","33","111"); 
var s = ary.join(",")+","; 
for(var i=0;i<ary.length;i++) { 
if(s.replace(ary[i]+",","").indexOf(ary[i]+",")>-1) { 
alert("数组中有重复元素:" + ary[i]); 
break; 
} 
}

Example 3

function isRepeat(arr){ 
var hash = {}; 
for(var i in arr) { 
if(hash[arr[i]]) 
return true; 
hash[arr[i]] = true; 
} 
return false; 
}


The above is the detailed content of How to determine whether a given variable value is contained in an array in javascript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn