Home >Web Front-end >JS Tutorial >Expansion and repair of arrays of reading notes in javascript framework design_javascript skills

Expansion and repair of arrays of reading notes in javascript framework design_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:29:371230browse

1.indexOf and lastIndexOf methods:

Because IE7 will report an error when using indexOf on an array object, it needs to be rewritten for compatibility.

Copy code The code 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) //Congruent judgment, indexOf, lastIndexOf
   return i;
return -1;
}

2.shuffle method: shuffle the array.

Copy code The code 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){} 
//Assume length=10, then Math.random()*10->[0,10), after parseInt, [0,9], randomly select one to exchange with the last item of the array. The second time through the loop, [0,8] is swapped with the penultimate item of the array.
return target;
}

3. Flattening of arrays: flatten, returning a one-dimensional array

Copy code The code 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 on array

Interviewers like to ask about this method the most, because it has many implementation methods, the most common one is two for loops. The most commonly known thing is to use an object a, and then a for loop array arr. Each time if (a[arr[i]]) exists, if it does not exist, it will be pushed to your newly defined array result. The existence is proved and repeated, so there is no need to push it to the result. In this scheme, "123" and 123 will be considered the same. In fact, one is a string and the other is a number, so they should not be considered the same.

So the following method appears: [1,"1","1"]

Copy code The code 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, save them. If they are equal, it proves that the value already exists before.

If the types are not the same, there are two situations,

In the first case, obj has already stored 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, for example: array[i] = "123", obj["123"] = undefind, then typeof obj[array[i]]) is typeof undefined = undefined, not equal to typeof array[i], stored in the array.

This method can solve the case where strings and numbers are the same, but it cannot solve the case where the objects are the same. For example: a = {1:2}, b ={2:1};

The first time through the loop, typeof obj[a] = undefined, typeof a = Object. Store obj[a] = a. In fact, it is 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 deposit

obj[b] = b; that is, obj[Object] = b; covering the previous obj[Object] = a;

In this case, all objects will have only the last object value stored.

When thinking about objects, I use this approach:

Copy code The code 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 you want to sort objects, you can write your own 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 and 7.

Copy code The code is as follows:
if([].unshift(1)!==1) //Add an item from the front to the empty array. Other browsers will return 1, but IE6 and 7 will 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. When splice takes one parameter, IE8 and below versions default the second parameter to 0, while other browsers use the array length.

Copy code The code is as follows:
if([1,2,3].splice(1).length == 0) //IE8 and below versions will be equal to 0, 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 the push, pop, shift, and unshift of the array (these methods will also modify the options of the array) will all call this method to implement it.

There is something to note here:

Copy code The code 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, start the operation on the yellow option. If the deletion is 0, the added option is inserted before yellow. Remember.


Here, please take a look at the difference between splice and slice, the return value, and the impact on the original array.
The above is a condensed version of the content of this section. Although it is concise, the key points are still there. I hope it will be helpful to everyone when reading this section

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:js time and date formatting encapsulation function_javascript skillsNext article:js time and date formatting encapsulation function_javascript skills

Related articles

See more