25.4.14

Array.prototype.unique and count



Array.prototype.unique = function() {
    var o = {}, i, l = this.length, r = [];
   
  for(i=0; i<l;i+=1) 

if(!o[this[i]]) o[this[i]] = {"key": this[i], "value":1};
else o[this[i]] = {"key": this[i], "value":o[this[i]].value+1};
   
for(i in o) 
r.push({"key": o[i].key, "value": o[i].value});
   
return r;
};

No comments:

Post a Comment