Return 数组中的值和按频率出现的值

Return values from array and occurences by frequency

所以我有这个代码

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

这几乎是我需要的,只是它没有 return 值出现的次数。

我试过重写,但我总是在排序部分失败。

感谢您的帮助

这应该可以满足您的要求:

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]= {item: p, frequency: o[p]};
    return a.sort(function(a, b){
        return o[b.item]-o[a.item];
    });
}

测试:

var A= ["apples","oranges","oranges","oranges","bananas","bananas","oranges"];
A.byCount()

产生:

[ { frequency: 4, item: "oranges" }, { frequency: 2, item: "bananas"}, {frequency: 1, item: "apples"} ]

编辑

正如 Bergi 在评论中指出的那样,return o[b.item]-o[a.item]; 完全过于复杂且毫无意义。 return b.frequency - a.frequency; 会更好。

您可以包括频率并通过执行以下操作稍微简化代码:

Array.prototype.byCount = function() {
  var o = {};

  this.filter(function(el) { return el; }).forEach(function(el) {
      o[el] = (o[el] || 0) + 1;
  });

  return Object.keys(o).map(function (key) {
      return { key: key, occurrences: o[key] };
  }).sort(function(a, b) {
    return b.occurrences - a.occurrences;
  });
}

console.log(JSON.stringify([1, 2, null, 9, 9, undefined, 9, 9, 1, 3, 1, 1, 9, 2].byCount()));

结果:

[{"key":"9","occurrences":5},
 {"key":"1","occurrences":4},
 {"key":"2","occurrences":2},
 {"key":"3","occurrences":1}]

如果你有 Lo-Dash 可用,你可以做得更干净一点:

Array.prototype.byFreq = function () {
  var grouped = _.groupBy(_.compact(this)),
      pairs = _.map(grouped, function (vals, key) { 
          return { key: key, occurrences: vals.length }
      }),
      sorted = _.sortBy(pairs, function (pair) { 
          return -pair.occurrences 
      });
  return sorted;
};

console.log(JSON.stringify([1,2,9,9, null, 9,9,1,3, undefined, 1,1,9,2].byFreq()));
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>

如果您想将出现次数添加到值中,您可以将它们放在最后:

return a.sort(function(a, b){
    return o[b]-o[a];
}).map(function(v) {
    return {value: v, count: o[v]}
});

或者直接将它们放入数组中并适配比较函数:

for (var p in o)
    a.push({value: p, count: o[p]});
return a.sort(function(a, b){
    return a.count-b.count;
});