A JS gets the array subscript function

Source: Internet
Author: User

Perhaps, during the interview, you may be asked how to simulate the string of indexof () function to get the subscript of the array, here is a small function of the individual implementation, which contains a lot of knowledge, if it is novice, can not understand the place Baidu, here do not do a detailed introduction, of course, do not understand can also give me a message , I'll get back to you when I'm free.

/*gets the index of the array element, the first parameter is the element of the group, it can be a composite type, but cannot be a function and a undefined, it cannot be null for the second parameter, the negative number is looked up from the back position, the direction of the lookup is still looking backwards, for example-1, is the last position to look backwards, that is, to match only the last element.*/Array.prototype.indexof=function(value,fromindex) {vararr = This. ValueOf (), len= This. Length; //returns False if Arr is not an array or if the first argument is empty or undefined        if(Tostring.call (arr)!== ' [object Array] ' | | value = = = ' | | value = = = Undefined | | tostring.call (value) = = = ' [Object funct Ion] '){            return false; }        //The default first parameter is 0        if(Fromindex = = =undefined) {Fromindex= 0; }        //The second parameter is not a number returns false        if(Tostring.call (Fromindex)!== ' [Object number] '){            return false; }        //determines whether the second parameter is a negative number        if(fromindex<0) {Fromindex=Math.Abs (Fromindex); //More search Scopes            if(Len <Fromindex) {                return-1; }Else{                //negative numbers are searched backwards from behindFromindex = Len-Fromindex; }        }        //Start Finding         for(vari=0+fromindex;i<len;i++){            if(Value = = =Arr[i]) {                returni; }Else{                //Judging data type equality                if(Tostring.call (arr[i]) = = =Tostring.call (value)) {                    //Judging data values equal                    if(Json.stringify (arr[i]) = = =json.stringify (value)) {                        returni; }Else{                        return-1; }                }                }        }        return-1; }    //Test    varARR1 = [1,9,0, ' W ', 4,5,[1,6],{' u ': 1, ' A ': 2},NULL]; varOB = {' U ': 1, ' a ': 2}; varOb1 = {' U ': 1, ' a ': 3}; varget=function(){}; varA = [1]; varb = [1,6]; Alert (Arr1.indexof (b));

  Still the old saying, if there is a mistake or bug, but also hope that crossing, lest mislead other people.

A JS gets the array subscript function

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.