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