Underscore.js:过滤对象数组,然后按平均排序?

时间:2016-02-16 19:06:02

标签: javascript underscore.js

我有一系列数据,如下所示,按月升序排序:

[{
  org_id: "K83002",
  percentile: 1,
  date: "2013-11-01",
},
{
  org_id: "K83059",
  percentile: 33,
  date: "2013-12-01",
},
{
  org_id: "K83607",
  percentile: 22,
  date: "2013-12-01",
} ...

我想创建一个排序的组织列表,按其在数据中最近三个月的平均百分位数排名。

现在,我正在这样做:

            var lastThreeMonths = _.uniq(_.pluck(data, 'date'), true).slice(-3);
            var averages = {};
            var lastMonths = _.each(data, function(d) {
                if (_.contains(lastThreeMonths, d.date)) {
                    if (d.org_id in averages) {
                        averages[d.org_id] += d.percentile;
                    } else {
                        averages[d.org_id] = d.percentile;
                    }
                }
            });
            var temp = [];
            for (var k in averages) {
                temp.push({ 'org_id': k, 'percentile': averages[k]});
            }
            var sortedAvgs = _.sortBy(temp, 'percentile').reverse();

但这非常冗长。有没有更好的方法呢?

理想情况下,我还想以某种方式为每个组织附加已排序数组的原始数据,因为我将在下一步迭代排序数组,并且访问迭代器内部的数据会很方便。

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

var sortedAverages = _.chain(data)
  .filter(d => _.contains(lastThreeMonths, d.date))
  .groupBy('org_id')
  .map(eachOrg => {
    var average =  _.sum(_.pluck(eachOrg, 'percentile'));
    return {
      org_id : eachOrg.org_id,
      percentile : average
    };
  })
  .sortBy('percentile')
  .value()
  .reverse();

答案 1 :(得分:0)

这可以帮到你:

// Create a hash of allowed dates - much improved performance over arrays for larger data sets
var lastThreeMonthsHash = _.chain(data).pluck('date').uniq().slice(-3).map(function (date) {
    return [date, true]
}).object().value();

// filter out older entries
var grouped = _.chain(data).filter(function (monthly) {
    return lastThreeMonthsHash[monthly.date];
    // group by org_id
}).groupBy('org_id').map(function (values, orgId) {
    // reduce each group into a single value which is the average percentile
    var percentiles = _.pluck(values, 'percentile');
    var sum         = _.reduce(percentiles, function (memo, value) {
        return memo + value;
    }, 0);
    return {
        org_id    : orgId,
        percentile: sum / values.length
    };
    // sort by percentile & reverse
}).sortBy('percentile').reverse().value();