JS Array and string to repeat several methods

Source: Internet
Author: User
Tags hasownproperty

JS Array to repeat the first of several methods: is also the most stupid bar.

Array.prototype.unique1 = function () {
var r = new Array ();
Label:for (var i = 0, n = this.length; i < n; i++) {
for (var x = 0, y = r.length; x < y; + +) {
if (r[x] = = This[i]) {
Continue label;
}
}
R[r.length] = this[i];
}
return R;
}
The second kind: This is the same as the heavenly book.

Array.prototype.unique2 = function () {
Return This.sort (). Join (",,"). Replace (/(, |^) ([^,]+) (,, \2) + (, |$)/g, "$1$2$4"). Replace (/,,+/g, ","). Replace (/,$/, "" ). Split (",");
}
The third type: Using the object's "hasOwnProperty" method

Array.prototype.unique3 = function () {
var temp = {}, len = This.length;
for (var i=0; i < Len; i++) {
var tmp = this[i];
if (!temp.hasownproperty (TMP)) {
Temp[this[i]] = "My God";
}
}

len = 0;
var temparr=[];
for (var i in temp) {
temparr[len++] = i;
}
return Temparr;
}
The fourth kind: first sorts, the preceding paragraph than latter. This method is quite simple, but also practical

Array.prototype.unique4 = function () {
var temp = new Array ();
This.sort ();
for (i = 0; i < this.length; i++) {
if (this[i] = = This[i+1]) {
Continue
}
Temp[temp.length]=this[i];
}
return temp;

}


The following is often used before, the efficiency is also very good. Feel a little bit like a hash table.

Array.prototype.unique5 = function () {
var res = [], hash = {};
For (var i=0, Elem; (Elem = This[i])! = NULL; i++) {
if (!hash[elem])
{
Res.push (Elem);
Hash[elem] = true;
}
}
return res;
You can also have a simpler and clearer notation:/* Apply a set of ideas, order not repeat */
function RemoveDuplicates (arr) {

var temp = {}, r = [];

for (var i in arr)

Temp[arr[i]] = true;

For (var k in temp)

R.push (k);

return R;

} looks pretty easy. After you have verified it, the code is as follows:
1 functionRemoveDuplicates (arr) {2 3     vartemp = {}, R = [];4 5      for(varIincharr)6 7Temp[arr[i]] =true;8 9      for(varKinchtemp)Ten  One R.push (k); A  -     returnR; -  the } -  - //usage -  + varFruits = [' apple ', ' orange ', ' peach ', ' apple ', ' strawberry ', ' orange ', ' strawberry ', ' orange ']; -  + varUniquefruits =removeduplicates (fruits); Aalert (uniquefruits);

String de-weight method:

The use of regular

/**
* String de-weight
* \ r \ n String delimiter
* The index of the split string, the $ $ string
* Split the string with a delimiter and cycle through the number of elements of the split array
*/
function Strunique () {
var str = "ABC, ABCD, ABC, ABCDE, ABCD, ABCDE";
var ret = [];
Str.replace (/[^,]+/g, function ($, $) {
(Str.indexof ($) = = $) && Ret.push ($);
});
alert (ret);
return ret;
}

1     functionStrunique () {2         varstr = "ABC, ABCD, ABC, ABCDE, ABCD, ABCDE";3         varRET = [];4Str.replace (/[^,]+/g,function($, $){5(Str.indexof ($) = = $) && Ret.push ($);6         });7 alert (ret);8         returnret;9     }Ten      OneStrunique ();

JS Array and string to repeat several methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.