Looking at the source code of YUI from time to time, there will always be some gains.
I. source code in YUI'
Copy codeThe Code is as follows:
Var toObject = function (){
Var o = {};
For (var I = 0, j =. length; I <j; I = I + 1) {// I have adjusted it here. The YUI source code is I <. length
O [a [I] = true;
}
Return o;
};
Var keys = function (o ){
Var a = [], I;
For (I in o ){
If (o. hasOwnProperty (I) {// here, the YUI source code is lang. hasOwnProperty (o, I)
A. push (I );
}
}
Return;
};
Var uniq = function (){
Return keys (toObject ());
};
Note: For ease of writing, I omit the class names before each method. You can check the YUI source code on your own. Both 2.7.0b and 2.8.0r4 can be used (the code in this part is the same ).
Ii. Ideas of this method
1. first, an object is generated with the value of the target array as the key. this step is the core: Because keys in an object cannot be repeated, so that repeated values are cleverly excluded;
2. traverse the generated objects and put these keys in an array. OK. This is done! (Easy, just two steps)
Iii. Features of this method
1. this method only traverses twice regardless of the number of items in the array, which has obvious performance advantages (think about your previous practice: Compare array items one by one, the performance is poor, you can imagine ).
2. in my tests, this method is only applicable to the one-dimensional array of strings and numbers (I think it is too easy to exclude repeated items for multi-dimensional arrays !).
For other methods, refer to the previous article.