JavaScript trap in statement

Source: Internet
Author: User

Recently in the company's project, back and forth in the background of data analysis together pick, the brain of the JS grammar and Python grammar big mess, so make a lot of people laugh and cry of the oolong, such as stepping on a JS hole tonight.

First, two graphs are compared (for Python statements, JavaScript):

Well, the in statement seems to be common to both PY and JS to determine whether an element exists in the array, however, the magic is still below (JavaScript code):

??? 3 not in array [3, 2, 1]???

???" A "is not in the array [" A "," B "]???

The problem is that in the JavaScript world, the role of in and in the Python world is different.

The in statement can be used in the Python world for a list object or for a Dict object (Val1 in Dict1 determines whether Val1 is in the collection of Dict1 keys).

However, the in statement is only available in JavaScript for object objects, using the correct usage such as:

So when writing JavaScript code, how do you tell if an element exists in an array object?

1. Traverse Dafa of the native JS method:

1 functionIn_array (searchstring,array) {2    for(i=0;i<array.length;i++) {3     if(SearchString = = Array[i])return true;4   }5   return false;6 }7 if(In_array (' oak ', Trees)) document.write (' true ');//Display True8   Elsedocument.write (' false ');

2. The proper use of the native JS method in the operator:

(since we know that in can be used to determine the property value of an object, we can also map array one by one to the object's properties, and then use in to judge.) )

1 functionOC (a)2 {3   varo = {};//equivalent to var o = new Object ();4    for(vari=0;i<a.length;i++)5   {6O[a[i]]= ";//Note that the wording cannot be written as O.a[i]7   }8   returno;9 }Ten if(' Oak 'inchOC (trees)) document.write (' true ');//Display True One   Elsedocument.write (' false '); An \oc (trees); - if(O.oak! = ' undefined ') document.write (' true ');//Display True -   Elsedocument.write (' false ');//true the if(o[' oak ']! = ' undefined ') document.write (' true ');//Display True -   Elsedocument.write (' false ');//true

Reference from: Https://snook.ca/archives/javascript/testing_for_a_v

3.jquery Dafa:

1 $.inarray (value, array)   

JavaScript trap in statement

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.