Last active
May 10, 2019 07:30
-
-
Save rao123dk/c3d59205d9260125e8097d10fe90709e to your computer and use it in GitHub Desktop.
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
(A)Array.prototype.unique = function(){ | |
return this.filter((value, index, self)=>( | |
self.indexOf(value) === index | |
)); | |
} | |
var names = ["dk", "rao","dk","jsk"]; | |
console.log(names.unique()); | |
(B) var j = [...new Set(names)] | |
console.log(j); | |
(C) Array.prototype.unique = function(){ | |
var self = this; | |
return this.filter(function(elem, index){ | |
return self.indexOf(elem) === index; | |
}) | |
} | |
console.log(names.unique()); | |
(D) console.log(Array.from(new Set(names))) | |
(E) Array.prototype.unique_ = Array.prototype.unique || function() { | |
var arr = []; | |
this.reduce(function (hash, num) { | |
if(typeof hash[num] === 'undefined') { | |
hash[num] = 1; | |
arr.push(num); | |
} | |
return hash; | |
}, {}); | |
return arr; | |
} | |
console.log(names.unique_()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment