Return values from array and occurences by frequency [duplicate]
问题 This question already has answers here : Counting the occurrences / frequency of array elements (36 answers) Closed 3 years ago . So i have this code Array.prototype.byCount= function(){ var itm, a= [], L= this.length, o= {}; for(var i= 0; i<L; i++){ itm= this[i]; if(!itm) continue; if(o[itm]== undefined) o[itm]= 1; else ++o[itm]; } for(var p in o) a[a.length]= p; return a.sort(function(a, b){ return o[b]-o[a]; }); } source This is almost what i need except that it does not return the number