This article mainly introduces the in_array function implemented by JavaScript, which is used to determine whether a value is in an array, similar to the in_array function of PHP, you can refer to JS to determine whether a value is directly used in an array without a function. For example, PHP contains the in_array () function. However, we can write a function similar to in_array () to determine whether a value is in the function.
/*** JS determines whether a value exists in the array * // defines 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 (! IsArr) {throw "arguments is not Array";} // indicates whether the traversal is in the Array for (var I = 0, k = arr. length; I
If the input is not an array, an exception is thrown.
/*** JS determines whether a value exists in the array * // defines 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 (! IsArr) {throw "arguments is not Array";} // indicates whether the traversal is in the Array for (var I = 0, k = arr. length; I
Direct error: