1. IndexOf and lastIndexOf methods:
Because IE7 will report an error using indexOf on an array object, it is necessary to rewrite a compatibility one.
The code copy is as follows:
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) //Consolidar judgment, indexOf, lastIndexOf
return i;
return -1;
}
2.shuffle method: shuffle the array.
The code copy is as follows:
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, then after Math.random()*10->[0,10),parseInt, [0,9], randomly select one and exchange it with the last item in the array. The second loop, [0,8], exchanges with the penultimate term of the array.
return target;
}
3. Flatten process of array: flatten, return a one-dimensional array
The code copy is as follows:
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: Deduplication operation of array
This method is the interviewer likes to ask about, because it has multiple implementation methods, the most common one is the two for loops. The most common thing I know is to use an object a and then a for loop array arr. Every time if (a[arr[i]]) exists, and if it does not exist, it will be pushed into your newly defined array result. Existence proves, repeats, so there is no need to push into the result. This solution, for "123", 123, will be considered the same. In fact, one is a string and the other is a number, and should not be considered the same.
So the following method appears: [1,"1","1"]
The code copy is as follows:
if ((typeof obj[array[i]]) != (typeof array[i]) || obj[array[i]] != array[i]) {
a.push(array[i]);
obj[array[i]] = array[i];
}
//First determine whether the types are the same. If they are the same, determine whether their values are equal. If they are not equal, they will be saved. If they are equal, they will prove that this value has existed before.
If the types are different, there are two situations here.
In the first case, obj has already saved this data before, for example: obj[123] = 123, now array[i] = "123". At this time, typeof obj[array[i]]) is a number, and typeof array[i] is a string, so it is stored in the array.
The second case is that obj has not saved this data yet, for example: array[i] = "123",obj["123"] = undefined. At this time, typeof obj[array[i]]) is typeof undefined = undefined, which does not equal typeof array[i] and is stored in the array.
This method can solve the situation where strings and numbers are the same, but cannot solve the situation where objects are the same. For example: a = {1:2}, b = {2:1};
When the first loop, typeof obj[a] = undefined, typeof a = Object. Deposit obj[a] =a. In fact, obj[Object] = a;
In the second loop, typeof obj[b] is equal to typeof obj[Object], which is actually typeof a = object,typeof b = object. Therefore, it enters obj[array[i]] != array[i]|, that is, obj[b]->obj[Object]->a! = b, so deposited
obj[b] = b; that is, obj[Object] = b; overwrites the previous obj[Object] = a;
In this case, all objects will appear, and only the last object value will be saved.
When considering objects, I use the following method:
The code copy is as follows:
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 sorting: sort method. If the object you want to sort is an object, you can write a compare(a,b){if(a.age>b.age) return 1;else return -1;},A.sort(compare).
6.min returns the minimum value of the array: return Math.min.apply(0,array);
7. unshift does not return the array length under ie6,7.
The code copy is as follows:
if([].unshift(1)!==1) //Add an item from the previous one to an empty array, other browsers will return 1, while IE6 and 7 will not return the array length. Execute the if statement
{
var _unshift = Array.prototype.unshift; //Function hijacking.
Array.prototype.unshift = function(){
_unshift.apply(this,arguments);
return this.length;
}
}
8.splice In the case of one parameter, the default second parameter of IE8 and the following versions is 0, while other browsers are array length.
The code copy is as follows:
if([1,2,3].splice(1).length == 0) //IE8 and the following versions will be equal to 0, and other versions will be equal to 3, enter if
{
var _splice = Array.prototype.splice;
Array.prototype.splice = function(a){
if(arguments.length == 1) //If there is only one parameter
{
return _splice.call(this,a,this.length);
}else{
return _splice.apply(this,arguments);
}
}
}
This method will change the options of the array, so push, pop, shift, and unshift of the array (these methods will also modify the options of the array) will call this method to implement it.
Here is a place to note:
The code copy is as follows:
var color = new Array('red','blue','yellow','black');
var color2 = color.splice(2,0,'brown','pink');
alert(color); // red,blue,brown,pink,yellow,black, on the yellow option, start the operation, if delete is 0, the added option is inserted before yellow. Remember.
Here, please check the difference between splice and slice, return value, and its impact on the original array.
The above is a simplified version of the content of this section. Although it is streamlined, the key points are all. I hope it will be helpful to everyone when reading this section.