Introduction to String.LastIndexOf () function usage in JavaScript

Source: Internet
Author: User

Grammar
var index = Array.lastindexof (searchelement[, Fromindex]);
Parameter description
Searchelement: Elements to search for

Fromindex: The location at which to start the search, the default length of the array, in which case, all the elements of the element will be searched. If the value passed in is greater than or equal to the range of the array (length), the entire array is searched, and if the passed-in value is less than 0, it returns-1, in which case the search operation will not be executed. The search is in the opposite direction.

Function description
Compares the absolute consistency (= = =) of each element of the searchelement and array, and returns the index of the current element when an element meets the criteria. If it is not found, return directly to-1.

In browsers that do not support this property, you can use the following code:

The code is as follows Copy Code
<script language= "JavaScript" type= "Text/javascript" >

Description: LastIndexOf () method for Javascript Array

if (! ARRAY.PROTOTYPE.LASTINDEXOF)
{
Array.prototype.lastIndexOf = function (ELT/*, from*/)
{
var len = this.length;

var from = number (arguments[1]);
if (isNaN (from))
{
from = len-1;
}
Else
{
From = (From < 0)
? Math.ceil (from)
: Math.floor (from);
if (from < 0)
from + = Len;
else if (from >= len)
from = len-1;
}

for (; from >-1; from--)
{
If (from, this && this[from] = = ELT)
return from;
}
return-1;
};
}
</script>

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.