IndexOf method in JavaScript Array

Source: Internet
Author: User
Use of the indexOf method String type in the JavaScript Array

Let's take a look at the well-known string usage.

let str = 'orange';  str.indexOf('o');  //0 str.indexOf('n');  //3 str.indexOf('c');  //-1

Here 0 and 3 are the positions where o and n appear in the string. The starting subscript is 0. -1 indicates that the request is not matched.

Someone once asked me why-1 is not null or undefined. Ask the person who makes the rule! Helpless.

We can see that there are no highlights here. Don't worry about another example.

let numStr = '2016';  numStr.indexOf('2');  //0 numStr.indexOf(2);  //0

Here, there is a small point: indexOf will perform a simple type conversion, convert the number into a string '2' and then execute.

Use of the Number type

You may wonder if there is any indexOf method for the number type because it will undergo implicit conversion! I clearly tell you no. The example above

let num = 2016;  num.indexOf(2);  //Uncaught TypeError: num.indexOf is not a function

Do I have to use the indexOf METHOD FOR THE number type? Convert it to a string, and then write it in the previous example.

// Second, the young man's Writing Method num = '000000'; num. indexOf (2); // 0 // num for normal youth. toString (). indexOf (2); // 0 // the style of young literature and art (''+ num ). indexOf (2); // 0

The first method is simple and straightforward. It is not feasible for known short numbers. But when the num variable changes for different data, what should I do?

The second method is the most commonly used, but it is a little longer than the third method. Haha, actually, it's okay. The third type of code cleanup is preferred.

Use of Array type

The big boss is here.

I am not familiar with the array method, but ignore the array's indexOf method (I personally think ).

What problems have you encountered? What are the points of attention?

let arr = ['orange', '2016', '2016'];  arr.indexOf('orange'); //0  arr.indexOf('o'); //-1  arr.indexOf('2016'); //1  arr.indexOf(2016); //-1

The examples are not so detailed here. The four use cases are sufficient to illustrate the problem.

Arr. indexOf ('Orange ') outputs 0 because 'Orange' is the 0th element of the array, matches and returns a subscript.

Arr. indexOf ('O') Output-1 because this method will not re-Execute indexOf matching on the basis of each element.

Arr. indexOf ('20140901') Outputs 1 because this method matches the table from the beginning until it is matched and returns the table of the first array element, instead of returning all matched subscript.

Arr. indexOf (2016) Output-1 Note: implicit type conversion is not performed here.

Since the pitfalls have been discovered, we may wish to answer the question. Go to the MDN official website to check the details. If you are interested in this topic, you can directly jump to Array. prototype. indexOf ()

The following is an official Description for anyone who just wants to know about it.

IndexOf () compares searchElement to elements of the Array using strict equality (the same method used by the === or triple-equals operator ).

Clearly, we use strictly equal to (= ). Pay attention to this when making similar judgments. Do not mistakenly think that numbers are converted into strings, and strings are not converted into numbers.

Summary

The accumulation of small knowledge points is not a topic for further discussion. Therefore, the second parameter indexOf () is not explained here. I believe everyone knows the role of the second parameter. If you do not know anything, refer to String here. prototype. indexOf (), and then take a look at the second parameter based on the link of the above array.

Related Article

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.