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

How to use lastindexof () in javascript

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to use lastindexof () in javascript. I hope you will get something after reading this article. Let's discuss it together.

In javascript, lastindexof () is used to find an element in an array, returning the last occurrence of the specified element value in the array (subscript value), with the syntax "array.lastIndexOf (item,start)"; if the return value is "- 1", the specified element does not exist in the array.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

In javascript, the lastindexof () method is used to find elements in an array.

The lastindexof () method returns the last occurrence of a specified element in the array (subscript or index value). If the element to be retrieved does not appear, the method returns-1.

Therefore, the lastindexof () method can be used to determine whether the specified value exists in the array.

Syntax:

Array.lastIndexOf (item,start)

Item is required. Specifies the string value to retrieve.

Start optional integer parameters. Specifies where to start the retrieval in the string. Its legal value is 0 to stringObject.length-1. If this parameter is omitted, the retrieval starts at the last character of the string.

The lastindexof () method retrieves the specified element item in the array from end to end. The retrieval starts at the start of the array or at the end of the array (when no start parameter is specified). If an item is found, the item is returned to retrieve the location of the first occurrence in the array from the tail forward. The index start position of the array starts at 0.

Example 1:

Var a = ["ab", "cd", "ef", "ab", "cd"]; console.log (a.lastIndexOf ("cd")); / / 4console.log (a.lastIndexOf ("cd", 2)); / / 1console.log (a.lastIndexOf ("gh")); / /-1console.log (a.lastIndexOf ("ab",-3))

Example 2: determine whether the specified value exists

Var a = ["ab", "cd", "ef", "ab", "cd"]; var b = "cd"; if (a.lastIndexOf (b) =-1) {console.log ("element does not exist");} else {console.log ("element does not exist");} var b = "gh"; if (a.lastIndexOf (b) =-1) {console.log ("element does not exist") } else {console.log ("element exists");}

After reading this article, I believe you have a certain understanding of "how to use lastindexof () in javascript". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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: 226

*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