JavaScript-implemented in_array function _ javascript tips-js tutorial

Source: Internet
Author: User
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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.