In JS, You need to determine whether a value is directly used by no function in the array. For example, the in_array () function exists in PHP. You can write a function similar to in_array () function Methods in Javascript need to determine whether a value is used directly in an array without a function, such as the in_array () function in PHP. However, we can write a function similar to in_array () to determine whether a value is in the function.
Example 1
The Code is as follows:
/*
*
* Determines whether the array contains a given variable value.
* Parameters:
* Needle: value to be queried
* Haystack: The queried array.
* Check whether needle exists in haystack. If yes, true is returned. Otherwise, false is returned.
* This function is only valid for characters and numbers.
*
*/
Function findnum (){
Var a = [1, 2]; // assume that a is an array and 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 ("the array contains a [" + I + "]:" + a [I]);
Else
Alert ("the array does not exist" + obj );
}
Example 2
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 a 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 ";
}
// Check whether the traversal is in the array
For (var I = 0, k = arr. length; I If (this = arr [I]) {
Return true;
}
}
// If it is not in the array, false is returned.
Return false;
}
// Add a prototype to the string
String. prototype. in_array = in_array;
// Add a prototype to the numeric type
Number. prototype. in_array = in_array;
// Declare an array
Var arr = Array ('blue', 'red', '123', '123 ');
// String test
Var str = 'red ';
Var isInArray = str. in_array (arr );
Alert (isInArray); // true
// Digital test
Var num = 119;
Var isInArray = num. in_array (arr );
Alert (isInArray); // false
If the input is not an array, an exception is thrown.
/**
* JS determines whether a value exists in an array
* Qiongtai blog
*/
// Define a judgment Function
Var in_array = function (arr ){
// Determine whether a 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 ";
}
// Check whether the traversal is in the array
For (var I = 0, k = arr. length; I If (this = arr [I]) {
Return true;
}
}
// If it is not in the array, false is returned.
Return false;
}
// Add a prototype to the string
String. prototype. in_array = in_array;
// Add a prototype to the numeric 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 checks whether duplicate values exist 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 ("repeated elements in the array:" + ary [I]);
Break;
}
}
Example 5
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;
}