【问题标题】:Return values from array and occurences by frequency [duplicate]从数组返回值并按频率出现[重复]
【发布时间】:2015-03-20 13:15:12
【问题描述】:

所以我有这个代码

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

这几乎是我所需要的,只是它不返回值的出现次数。

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

感谢您的帮助

【问题讨论】:

  • 请向我们展示您重写它的尝试,然后我们将能够在排序部分为您提供帮助。

标签: javascript sorting frequency


【解决方案1】:

这应该做你想做的:

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 在 cmets 中指出的那样,执行 return o[b.item]-o[a.item]; 完全是过于复杂且毫无意义。 return b.frequency - a.frequency; 会更好。

【讨论】:

  • 这太复杂了。
  • 这几乎可以肯定。我花了尽可能少的精力修改现有代码。
  • 它正在工作,只是一件事。我应该怎么做才能在排序过程之前删除所有出现 1 的项目?谢谢
  • 您可以在a[a.length] = ... 之前使用if (o[p] != 1) 将它们从结果a 中排除
  • 那是因为排序函数应该是b.frequency - a.frequency
【解决方案2】:

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

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()));
&lt;script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"&gt;&lt;/script&gt;

【讨论】:

    【解决方案3】:

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

    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;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-23
      • 2019-08-15
      • 2015-07-01
      • 2021-05-13
      • 2013-06-24
      • 1970-01-01
      • 2015-06-28
      • 1970-01-01
      相关资源
      最近更新 更多