lastIndexOf does not work in Internet Explorer

I have the following code:
var currentServerlist = [];
    var newServerIp = document.getElementById('add_server').value; 
    if( CurrentServerIP != newServerIp )
    {
        $('#failoverServers td.row_selector').each(function() {
        var row = $(this).closest('tr');
        var serverIp = row.find('td[rel=ip]').text();
        currentServerlist.push(serverIp);	
        });

        if(currentServerlist.lastIndexOf(newServerIp) != -1)
        {
            return true;
        }
        return false;

Open in new window


But I find that the lastIndexOf does not work in InternetExplorer (It does in Chrome).

How can I fix this?
Wanting2LearnManAsked:
Who is Participating?
 
Meir RivkinConnect With a Mentor Full stack Software EngineerCommented:
add it to your javascript file:

if (!Array.prototype.lastIndexOf) {
Array.prototype.lastIndexOf= function(obj, start) {
var index=-1;
     for (var i = (start || 0), j = this.length; i < j; i++) {
         if (this[i] === obj) { index = i; }
     }
     return index;
}
}

Open in new window

0
 
RashadhourieConnect With a Mentor Commented:
The lastIndexOf() method is not supported in Internet Explorer 8 and earlier, you'll need  sedgwick's solution
0
 
Meir RivkinConnect With a Mentor Full stack Software EngineerCommented:
both indexOf and lastIndexOf are not supported.
btw, i based my solution on this:

how-to-fix-array-indexof-in-javascript-for-ie-browsers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.