Skip to content

Instantly share code, notes, and snippets.

@rao123dk
Last active May 10, 2019 07:30
Show Gist options
  • Save rao123dk/c3d59205d9260125e8097d10fe90709e to your computer and use it in GitHub Desktop.
Save rao123dk/c3d59205d9260125e8097d10fe90709e to your computer and use it in GitHub Desktop.
(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