Last active
December 19, 2015 16:39
-
-
Save kazua/5985300 to your computer and use it in GitHub Desktop.
Arrayのcontainsとdistinct
This file contains hidden or 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
| //write kazua | |
| if (!Array.prototype.contains) { | |
| Array.prototype.contains = function(value) { | |
| for ( var i = 0; i < this.length; i++) | |
| if (this[i] === value) | |
| return true; | |
| return false; | |
| }; | |
| } | |
| if (!Array.prototype.distinct) { | |
| Array.prototype.distinct = function() { | |
| var t = Object(this), nt = [], len = t.length >>> 0; | |
| if (len == 0) | |
| return this; | |
| for ( var i = 0; i < t.length; i++) | |
| if (!nt.contains(t[i])) | |
| nt.push(t[i]); | |
| return nt; | |
| }; | |
| } | |
| function test() { | |
| var a = new Array("1", "3", "4", "1", "5"); | |
| a = a.distinct(); | |
| alert(a); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment