1.indexOf and LastIndexOf methods:
Because IE7 uses indexof on an array object, it will be an error, so you need to override a compatibility.
Copy Code code 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)//congruent judgment, Indexof,lastindexof
return i;
return-1;
}
2.shuffle method: Shuffle the array.
Copy Code code 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) {}
Suppose length=10, then Math.random () *10->[0,10), after parseint, [0,9], randomly select one to exchange with the last item in the array. The second cycle, [0,8], is exchanged with the penultimate item of the array.
return target;
}
3. The flattening of the array: Flatten, returns a one-dimensional array
Copy Code code 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: Log group to redo operation
This method, the interviewer most like to ask, because it has a variety of implementation methods, the most common is two for loops. The most commonly known is the use of an object A, then a for loop array arr, each time if (A[arr[i]]) exists, does not exist then push to the new definition of the array result. Existence is proof, repetition, so no push to result. This scheme, for "123", 123, will think the same, in fact, one is a string, one is a number, should not be considered the same.
So the following methods appear: [1, "1", "1"]
Copy Code code as follows:
if ((typeof Obj[array[i]])!= (typeof Array[i]) | | obj[array[i]]!= Array[i]) {
A.push (Array[i]);
Obj[array[i]] = array[i];
}
The first is to determine whether the type is the same, if the same, to determine whether their values are equal, unequal to be stored in, equality is to prove that the value existed before.
If the types are not the same, there are two situations where
In the first case, OBJ has previously saved this data, such as: obj[123] = 123, now array[i] = "123", at which point, 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, such as: array[i] = "123", obj["123"] = Undefind, then typeof Obj[array[i]) is typeof undefined = undefined, is not equal to typeof Array[i] and is stored in an array.
In this way, you can resolve the same case with strings and numbers, but you cannot resolve the same situation as the object. For example: a = {1:2}, b ={2:1};
The first time the loop, typeof obj[a] = undefined,typeof a = Object. deposited in Obj[a] =a. Actually is obj[object] = A;
The second cycle, typeof obj[b] equals typeof Obj[object] is actually typeof a = object,typeof B = Object. So go to obj[array[i]!= array[i]|, that is obj[b ]->obj[object]->a! = B, so deposit
OBJ[B] = b; i.e. Obj[object] = B; covering the previous obj[object] = A;
In this case, all objects will appear, and only the last object value will be saved.
When I think about objects, I use the following method:
Copy Code code 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 you want to sort the object, you can write a compare (a,b) {if (a.age>b.age) return 1;else return-1;},a.sort (Compare) yourself.
6.min returns the array minimum value: Return Math.min.apply (0,array);
7.unshift does not return the array length under ie6,7.
Copy Code code as follows:
if ([].unshift (1)!==1)//The previous item is added to the empty array, the other browser returns 1, and ie6,7 does not return the array length. Then 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 a parameter, the IE8 and the following versions default the second argument to 0, while the other browsers are array lengths.
Copy Code code as follows:
if ([1,2,3].splice (1). length = = 0)//ie8 and the following version will be equal to 0, the other version 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 parameter
{
Return _splice.call (this,a,this.length);
}else{
Return _splice.apply (this,arguments);
}
}
}
This method changes the array's options, so the push,pop,shift,unshift of the array (which also modifies the array's options) will call this method to implement.
Here's one place to note:
Copy Code code 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, and if deleted is 0, the added option is to insert before yellow. Remember.
Here, let's look at the difference between splice and slice, the return value, and the effect on the original array.
The above is a concise version of the content of this section, although streamlined, but the emphasis is on, I hope to read this section of the time can be helpful