Home > Web Front-end > JS Tutorial > Determining whether a value is in an array in JavaScript is not directly used_Basic knowledge

Determining whether a value is in an array in JavaScript is not directly used_Basic knowledge

WBOY
Release: 2016-05-16 17:46:17
Original
1126 people have browsed it
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

Copy code The code is as follows:


/*
*
* Determine whether the array contains a given variable value
* Parameters:
* needle: the value to be queried
* haystack: the queried array
* in haystack Query whether needle exists, return true if found, otherwise return false.
* This function is only valid for characters and numbers
*
*/

function findnum(){
var a=[1,2];//Assume a is Array, obj is the number to be judged
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 "] exists in the array:" a [i]);
else
alert("does not exist in array" obj);
}

Example 2
Copy code The code is as follows:

/**
* JS determines whether a value exists in an array
* Qiongtai Blog
*/

// Define a judgment function
var in_array = function(arr){
// Determine whether the parameter is an array
var isArr = arr && console.log(
typeof arr==='object' ? arr.constructor===Array ? arr .length ? arr.length===1 ? arr[0]:arr.join(','):'an empty array': arr.constructor: typeof arr
);

// If it is not an array, an exception will be thrown
if(!isArr){
throw "arguments is not Array";
}

// Traverse whether it is in the array
for(var i =0,k=arr.length;iif(this==arr[i]){
return true;
}
}

// If it is not in the array, it will return false
return false;
}

// Add a prototype to the string
String.prototype.in_array = in_array;
// Give Add prototype for numeric type
Number.prototype.in_array = in_array;

// Declare an array
var arr = Array('blue','red','110','120') ;

//String test
var str = 'red';
var isInArray = str.in_array(arr);
alert(isInArray); // true

// Numeric test
var num = 119;
var isInArray = num.in_array(arr);
alert(isInArray); // false
will be thrown if the passed in is not an array Exception occurred
/**
* JS determines whether a value exists in an array
* Qiongtai Blog
*/

// Define a judgment function
var in_array = function(arr){
// Judge whether the parameter is an array
var isArr = arr && console.log(
typeof arr==='object' ? arr.constructor===Array ? arr.length ? arr.length===1 ? arr[0]:arr.join(' ,'):'an empty array': arr.constructor: typeof arr
);

// If it is not an array, an exception will be thrown
if(!isArr){
throw "arguments is not Array";
}

// Traverse whether it is in the array
for(var i=0,k=arr.length;iif( this==arr[i]){
return true;
}
}

// If it is not in the array, it will return false
return false;
}

// Add a prototype to the string
String.prototype.in_array = in_array;
// Add a prototype to the number type
Number.prototype.in_array = in_array;

// Declare an array
var arr = null;

// String test
var str = 'red';
var isInArray = str.in_array(arr);
alert(isInArray); // uncaught exception: arguments is not Array
JS determines whether there are duplicate values ​​in an array
var ary = new Array("111","22","33","111 ");
var s = ary.join(",") ",";
for(var i=0;iif(s.replace(ary [i] ",","").indexOf(ary[i] ",")>-1) {
alert("There are duplicate elements in the array: " ary[i]);
break ;
}
}

Example 5
Copy code The code is as follows:

function isRepeat(arr){
var hash = {};
for(var i in arr) {
if(hash[arr[i]])
return true;
hash[arr[i]] = true;
}
return false;
}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template