Home > Web Front-end > JS Tutorial > How to implement in_array using js_basic knowledge

How to implement in_array using js_basic knowledge

WBOY
Release: 2016-05-16 17:17:30
Original
1540 people have browsed it

In js, we cannot directly judge a value or string by using in_array in an array like in php, because js does not have an in_array function, but we can customize an in_array function for js. I have collected a few below .
Example 1

Copy code The code is as follows:

function in_array(stringToSearch, arrayToSearch) {
for (s = 0; s < arrayToSearch.length; s ) {
thisEntry = arrayToSearch[s].toString();
if (thisEntry == stringToSearch) {
return true ;
}
}
return false;
}

Example 2
Copy code The code is as follows:

var a = Array(1,2,3,4,5);
function in_array(search,array){
for( var i in array){
                                                                                                                                                                                                                                                                         (11,a));


Example 3



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){
// 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 is 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 = Array('blue', 'red','110','120');
// String test
var str = 'red';
var isInArray = str.in_array(arr);
alert(isInArray ); // true
//Number test
var num = 119;
var isInArray = num.in_array(arr);
alert(isInArray); // false
if passed in If it is not an array, an exception will be thrown
/**
* 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;
}
// Give the character Add prototype to string
String.prototype.in_array = in_array;
// Add prototype to 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;
}
}


Summary
The three examples all implement the function of in_array function, and they can all determine whether a given element is in the array. My personal favorite is the last method, which is more comprehensive.
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