The javascript framework is designed to extend and repair the array of reading notes

  • 2020-03-30 04:27:33
  • OfStack

1. Methods of indexOf and lastIndexOf:

Because IE7 USES indexOf on array objects to report an error, you need to override a compatible one.


Array.prototype.lastIndexOf(item,index){
  var n = this.length,i = (index==null||index>n-1)?n-1:index;
  if(i < 0) i = n+i;
  for(;i>=0;i--)
    if(this[i] === item)   //Judgment of congruence, indexOf, lastIndexOf
      return i;
  return -1;
}

2. Shuffle method: shuffle the array.


function shuffle(target){
  var i = target.length, j ,temp;
  for(;i>0;j=parseInt(Math.random() * i), x = target[--i],target[i] = target[j],target[j]=x){}   
     //Assuming length=10, math.random ()*10-> [0,10), after parseInt, [0,9], randomly select one to exchange with the last item of the array   return target;
}

3. Flatten the array: flatten, returns a one-dimensional array


function flatten(arr){
  var result = [];
  arr.forEach(function(item){
    if(Array.isArray(item))   result.concat(flatten(item));
    else  result.push(item);
  });
  return result;
}

4. Unique method: unduplicate the array

This method is a favorite to ask because there are many ways to implement it, the most common being two for loops. The most commonly known is to use an object a, and then a for loop array arr, each time if(a[arr[I]]) exists or not, push it into your newly defined array result. It exists to prove, and it repeats, so you don't have to push it into the result. This scenario, for "123", for "123", will think the same, one is a string, one is a number, should not be thought of as the same.

So the following methods appear: [1,"1","1"]


 if ((typeof obj[array[i]]) != (typeof array[i]) || obj[array[i]] != array[i]) {
  a.push(array[i]);
  obj[array[i]] = array[i];
}

// first, determine if the types are the same. If they are the same, determine if their values are equal. If they are not, store them.

If the type is different, there are two cases,

In the first case, obj has stored this data before, for example :obj[123] = 123, now array[I] = "123", typeof obj[array[I]]) is a number, and typeof array[I] is a string, so it is stored in an array.

The second case is that obj has not stored this data, for example: array[I] = "123",obj["123"] = undefind, in this case, typeof obj[array[I]) is typeof undefined = undefined, not equal to typeof array[I], into the array.

This method can solve the case of the same string and number, but not the case of the same object. For example :a ={1:2}, b ={2:1};

The first time around, typeof obj[a] = undefined, typeof a = Object. Store obj[a] =a. in fact, obj[Object] =a;

The second time around,typeof obj[b] = typeof obj[Object] = typeof a = Object,typeof b = Object. = array[I]|, that is, obj[b]-> Obj [Object] - > A!!!! = b, so store it

Obj [b] = b; So obj[Object] = b; Overwrite the previous obj[Object] = a;

In this case, all objects will appear, and only the last object value will be stored.

When considering objects, I use the following method:


for(var i = 0; i < temp.length; i++){
                for(var j = i + 1; j < temp.length; j++){
                        if(temp[i] === temp[j]){
                                temp.splice( j, 1 );
                                j--;
                        }
                }
        }
 return temp;

5. Array sort: sort method, if you want to sort objects, you can write a compare(a,b){if(a. ge> B.a ge) return 1; The else return - 1; }, A.s ort (compare).

Return math.min. apply(0,array);

7. Unshift does not return array length under ie6,7.


if([].unshift(1)!==1)   //Add an item to an empty array from the front and other browsers will return 1. IE6 and 7 will not return the array length. The if statement
is executed {
  var _unshift = Array.prototype.unshift;      //Function hijack. < br / >   Array.prototype.unshift = function(){
    _unshift.apply(this,arguments);
    return this.length;
  }
}

Splice with one parameter, IE8 and the following versions default to a second parameter of 0, while other browsers are array length.


if([1,2,3].splice(1).length == 0)   //IE8 and the following versions will be equal to 0, other versions will be equal to 3, into the if
{
  var _splice = Array.prototype.splice;
  Array.prototype.splice = function(a){
    if(arguments.length == 1)   //
if there is only one argument     {
      return _splice.call(this,a,this.length);
    }else{
      return _splice.apply(this,arguments);
    }
  }
}

This method changes the array's options, so push,pop,shift, and unshift (which also modify the array's options) all call this method.

Here's one thing to note:


var color = new Array('red','blue','yellow','black');
var color2 = color.splice(2,0,'brown','pink');
alert(color); //Red, blue, brown, pink, yellow, black, yellow option on, began operation, if you remove is 0, then add the option is inserted before the yellow. Be sure to keep in mind. < br / >

 
Here's a look at the difference between splice and slice, the return value, and the effect on the original array.


Related articles: