Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of indexOf and lastIndexOf Operation in JavaScript

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about the operation of indexOf and lastIndexOf in JavaScript. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Array.indexOf ()

The indexOf () method searches the array for element values and returns their positions.

Note: the location of the first project is 0, the location of the second project is 1, and so on.

Example

Retrieve the item "Apple" in the array:

Var fruits = ["Apple", "Orange", "Apple", "Mango"]

Var a = fruits.indexOf ("Apple")

Grammar

Array.indexOf (item, start)

Item is required. The item to retrieve.

Start is optional. Where to start the search. A negative value starts at the given position at the end and searches to the end.

If no item is found, Array.indexOf () returns-1.

If the project appears more than once, return to the location where it first appeared.

Array.lastIndexOf ()

Array.lastIndexOf () is similar to Array.indexOf (), but the search starts at the end of the array.

Example

Retrieve the item "Apple" in the array:

Var fruits = ["Apple", "Orange", "Apple", "Mango"]

Var a = fruits.lastIndexOf ("Apple")

All browsers support Array.lastIndexOf (), except for Internet Explorer 8 or earlier:

Grammar

Array.lastIndexOf (item, start)

Item is required. The item to retrieve.

Start is optional. Where to start the search. A negative value will start at the given position at the end and search to the beginning.

These are all the contents of this article entitled "how to operate indexOf and lastIndexOf in JavaScript". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report