JavaScript returns elements that are not duplicated in an array _javascript tips

Source: Internet
Author: User
This is part of the code that implements the structure pseudo class Type-of-type:
<script type= "Text/javascript" > var ret= ["Span", "span", "strong", "span", "B"] var norepeat = function (array) {var Set = Array.join (",") + ","; while (Array.Length) {var el = Array.shift (); Set = Set.replace (el+ ",", ""); if (Set.indexof (el+ ",")!=-1) {set = Set.replace (New RegExp (el+ ",", "G"), ""); Array = Set.replace (/,$/g, ""). Split (","); }else{set = el+ "," +set; } return Set.replace (/,$/g, ""). Split (",")} alert (Norepeat (ret))//last returned B,strong </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Feel the effect is not high, to worry-free master for help. Here is the customer service fruit (good and powerful replacement AH):
<script language= "JavaScript" > var ret= ["Span", "span", "strong", "span", "B"]; var norepeat=function (x) {var y= ' C0CB5F0FCF239AB3D9C1FCD31FFF1EFC '; Return X.sort (). Join (y). Replace (RegExp (' (. * (' +y+ ' |$)) \\1+ ', ' g '), '). Split (y); }; Alert (Norepeat (ret)); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Abcdreamer implementation (This implementation can filter the repetition in an array of impure strings, I prefer):
<script language= "JavaScript" > var a = ["span", "span", "strong", "span", "B"]; function Norepeat (a) {var i = a.length, j, B; while (i--) {j = i; b = false; while (j--) {if (a[i] = = A[j]) {A.splice (J, 1); i--; B = true; } if (b) A.splice (I, 1); return A; Alert (Norepeat (a)); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Which Master has a better implementation, please let me know!
It seems that some people do not understand the difference between the Norepeat function and the unique function, Norepeat is to remove the duplicate elements in the array, if there are two a in the array, then remove the two, the unique will remove one of them. Here is the implementation of the unique function:
<script language= "JavaScript" > Array.prototype.inArray = Function (EL) {for (Var i=0,n=this.length;i<n;i++) if (This[i]===el) return true; return false; } Array.prototype.unique = function () {var i = 0, n = this.length,ret = []; for (; i < n; i++) if (!ret.inarray (this[i)) Ret.push (this[i)); return ret; }; var ret= ["Span", "span", "strong", "span", "B"] alert (Ret.unique ())//Back to "span", "strong", "B" </script>
[ctrl+a All selected note: If you need to introduce external JS need to refresh to perform]
Related Article

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.