Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Searching Through an Array in Js

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 183
    Comment on it

    Hello reader's in this tutorial we will find an array by searching. We will search an specific array's value and get the array element index if found.


    We are going to use a Array object methods indexOf and lastIndexOf which is in the new (ECMAScript 5) :

     

    Syntax:-

    var animals = new Array("dog","cat","seal","elephant","walrus","lion");
    alert(animals.indexOf("elephant")); // prints 3

     

    With the release of ECMAScript 5 the support for both indexOf and lastIndexOf has existed in browsers for some time. Both methods takes a value of search and then compare to every element in the array. Both return and index which represent the array element. Value of "-1" is returned when the value is not found. The first one is found by using the "indexOf" method and last on is found by using the "lastIndexOf" method.

     

    Syntax:-

    var animals = new Array("dog","cat","seal","walrus","lion", "cat");
    alert(animals.indexOf("cat")); // prints 1
    alert(animals.lastIndexOf("cat")); // prints 5

     

    Both methods can take a starting index and set it where the search is starting:

     

    Syntax:-

    var animals = new Array("dog","cat","seal","walrus","lion", "cat");
    alert(animals.indexOf("cat",2)); // prints 5
    alert(animals.lastIndexOf("cat",4)); // prints 1

     

    Note :- Currently all the modern target browsers supports indexOf and lastIndexOf except in IE8.

     

    As mentioned before the indexOf and lastIndexOf method not supporter by all the browsers. A cross browser method will implement like functionality unsupported browsers which is given in Mozilla documentation at https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf.
     
    Thus, IE8 do not support indexOf, here is the Mozilla function for the support:
     

    if (!Array.prototype.indexOf){
        Array.prototype.indexOf = function(elt /*, from*/){
            var len = this.length >>> 0;
            var from = Number(arguments[1]) || 0;
            from = (from < 0)
                ? Math.ceil(from)
                : Math.floor(from);
            if (from < 0)
                from += len;
            for (; from < len; from++){
                if (from in this &&
                this[from] === elt)
                return from;
            }
            return -1;
        };
    }

    This function will make support for the IE8 browsers also.

    Hope this tutorial will help you to understand the method of indexOf and lastIndexOf..

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: