-
-
Save phoetry/1423014 to your computer and use it in GitHub Desktop.
Cross-browser Array.prototype.unique
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.unique = | |
function() | |
{ | |
for(var | |
a = this.slice().sort(), // sort this array | |
c = a.length, // length of sorted array | |
b = []; // result array | |
c--; // loop | |
a[c] === a[c-1] || // if current item is equal with neighbor, do not | |
b.push(a[c]) // push it | |
); | |
return b; // return the result | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 122 bytes | |
Array.prototype.unique=function(){for(var a=this.slice().sort(),b=[],c=a.length;c--;a[c]===a[c-1]||b.push(a[c]));return b} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "Cross-browser Array.prototype.unique", | |
"keywords": [ | |
"array", | |
"unique", | |
"sort" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Expected value: <b>undefined</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
Array.prototype.unique=function(){for(var a=this.slice().sort(),b=[],c=a.length;c--;a[c]===a[c-1]||b.push(a[c]));return b} | |
var arr = ['f','h','j','s','h','f','j','s','h','f','g','a','h','s','f','i','a','h','f','i','w','a','o','h','f','p','a','f','h','d','s','f','j','h']; | |
document.getElementById( "ret" ).innerHTML = arr.unique().join(', '); | |
</script> |
So what about mine?
for 140byt.es, we usually assume only ES3. ES5 makes it too easy!
@jed: But isn't yours uses ES5 also?
Array.filter() is ES5 according to MDN.
true, but @atk's version is not.
Link?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see also: https://gist.github.com/1044540