按多场分组Json数据

时间:2016-06-21 11:10:42

标签: javascript underscore.js grouping

我有一个JSON数据,其中包含一些成员作为俱乐部会费的付款清单。我正在尝试使用 underscore.js 库对数据进行分组,并总结数据。

我的示例JSON数据在下面提供。

[{
  month: 'January',
  total: 'to be the amount of all amount paid in January'
  year: 1995
}, {
  month: 'February',
  total: 'to be the amount of all amount paid in January'
  year: 1995
}, {
  month: 'March',
  total: 'to be the amount of all amount paid in January'
  year: 1995
}]

请,我想按月汇总付款密钥数组中的金额总额。

我希望能够按月分组,同时汇总一年中每个月支付的金额。我想要这种格式的数据。

CREATE TABLE t1 (
  id serial PRIMARY KEY,
  name text
);

CREATE TABLE t2 (
  id serial PRIMARY KEY,
  name text,
  foo text
);

CREATE FUNCTION gen_t2_foo(_a t1, _b t2) RETURNS text AS $$
  SELECT _a.name || ' - ' || _b.name;
$$ LANGUAGE sql;

CREATE FUNCTION upd_t2(_min_id int, _max_id int, _a t1) RETURNS VOID AS $$
  UPDATE t2 SET
    foo = gen_f2_name(_a, ???) -- How to pass the current t2 record?
    WHERE id >= _min_id AND id <= _max_id;
$$ LANGUAGE sql;

感谢您提供帮助。

2 个答案:

答案 0 :(得分:2)

最简单的方法是创建自己的分组和散列函数。

&#13;
&#13;
var dues = retrieveData();

var grouped = dues.reduce(function(groupedData, member, index, members) {
  return member.payment.reduce(function(memberData, payment) {
    var key = payment.month + '_' + payment.year;
    if (memberData[key] == null) {
      memberData[key] = {
        month : payment.month,
        total: payment.amount || 0,
        year: payment.year
      };
    } else {
      memberData[key].total += payment.amount; // Update payment.
    }
    return memberData;
  }, groupedData);
}, {});

var valueArr = Object.keys(grouped).map(function(key) {
  return grouped[key];
});

console.log(valueArr);


function retrieveData() {
  return [ {
    memberid: 194,
    payment: [
      { month: 'January', amount: 2500, year: 2015 },
      { month: 'February', amount: 2500, year: 2015 },
      { month: 'March', amount: 2500, year: 2015 },
      { month: 'April', amount: 2500, year: 2015 },
      { month: 'May', amount: 2500, year: 2015 },
      { month: 'June', amount: 2500, year: 2015 },
      { month: 'July', amount: 2500, year: 2015 },
      { month: 'August', amount: 2500, year: 2015 },
      { month: 'September', amount: 2500, year: 2015 },
      { month: 'October', amount: 2500, year: 2015 },
      { month: 'November', amount: 2500, year: 2015 },
      { month: 'December', amount: 2500, year: 2015 },
      { month: 'March', amount: 2500, year: 2016 },
      { month: 'May', amount: 2500, year: 2016 },
      { month: 'September', amount: 2500, year: 2016 }
    ],
    name: 'Makey Trashey'
  }, {
    memberid: 156,
    payment: [
      { month: 'January', amount: 2500, year: 2015 },
      { month: 'February', amount: 2500, year: 2015 },
      { month: 'March', amount: 2500, year: 2015 },
      { month: 'April', amount: 2500, year: 2015 },
      { month: 'May', amount: 2500, year: 2015 },
      { month: 'June', amount: 2500, year: 2015 },
      { month: 'July', amount: 2500, year: 2015 },
      { month: 'August', amount: 2500, year: 2015 },
      { month: 'September', amount: 2500, year: 2015 },
      { month: 'October', amount: 2500, year: 2015 },
      { month: 'November', amount: 2500, year: 2015 },
      { month: 'December', amount: 2500, year: 2015 },
      { month: 'March', amount: 2500, year: 2016 },
      { month: 'May', amount: 2500, year: 2016 },
      { month: 'July', amount: 2500, year: 2016 }
    ],
    name: 'Makey Johnny'
  } ];
};
&#13;
&#13;
&#13;

输出

[
  { "month": "January",    "total": 5000,  "year": 2015 },
  { "month": "February",   "total": 5000,  "year": 2015 },
  { "month": "March",      "total": 5000,  "year": 2015 },
  { "month": "April",      "total": 5000,  "year": 2015 },
  { "month": "May",        "total": 5000,  "year": 2015 },
  { "month": "June",       "total": 5000,  "year": 2015 },
  { "month": "July",       "total": 5000,  "year": 2015 },
  { "month": "August",     "total": 5000,  "year": 2015 },
  { "month": "September",  "total": 5000,  "year": 2015 },
  { "month": "October",    "total": 5000,  "year": 2015 },
  { "month": "November",   "total": 5000,  "year": 2015 },
  { "month": "December",   "total": 5000,  "year": 2015 },
  { "month": "March",      "total": 5000,  "year": 2016 },
  { "month": "May",        "total": 5000,  "year": 2016 },
  { "month": "September",  "total": 2500,  "year": 2016 },
  { "month": "July",       "total": 2500,  "year": 2016 }
]

通用方法

下面,我将数据与上面的代码分离,并将逻辑放入自己的函数中。它远非完美,但它是一个开始。

&#13;
&#13;
var dues = retrieveData();
var valueArr = groupData(dues, 'payment', ['month', 'year'], 'amount', 'total');

console.log(valueArr);

function groupData(data, targetProp, keys, accumFrom, accumTo) {
  var grouped = data.reduce(function(groupedData, item, index, items) {
    return item[targetProp].reduce(function(obj, record) {
      var key = keys.map(function(key) { return record[key]; }).join('_');
      if (obj[key] == null) {
        obj[key] = keys.reduce(function(data, key) {
          data[key] = record[key];
          return data;
        }, {});
      }
      obj[key][accumTo] = (obj[key][accumTo] || 0) + record[accumFrom];
      return obj;
    }, groupedData);
  }, {});

  return Object.keys(grouped).map(function(key) {
    return grouped[key];
  });
}

function retrieveData() {
  return [ {
    memberid: 194,
    payment: [
      { month: 'January', amount: 2500, year: 2015 },
      { month: 'February', amount: 2500, year: 2015 },
      { month: 'March', amount: 2500, year: 2015 },
      { month: 'April', amount: 2500, year: 2015 },
      { month: 'May', amount: 2500, year: 2015 },
      { month: 'June', amount: 2500, year: 2015 },
      { month: 'July', amount: 2500, year: 2015 },
      { month: 'August', amount: 2500, year: 2015 },
      { month: 'September', amount: 2500, year: 2015 },
      { month: 'October', amount: 2500, year: 2015 },
      { month: 'November', amount: 2500, year: 2015 },
      { month: 'December', amount: 2500, year: 2015 },
      { month: 'March', amount: 2500, year: 2016 },
      { month: 'May', amount: 2500, year: 2016 },
      { month: 'September', amount: 2500, year: 2016 }
    ],
    name: 'Makey Trashey'
  }, {
    memberid: 156,
    payment: [
      { month: 'January', amount: 2500, year: 2015 },
      { month: 'February', amount: 2500, year: 2015 },
      { month: 'March', amount: 2500, year: 2015 },
      { month: 'April', amount: 2500, year: 2015 },
      { month: 'May', amount: 2500, year: 2015 },
      { month: 'June', amount: 2500, year: 2015 },
      { month: 'July', amount: 2500, year: 2015 },
      { month: 'August', amount: 2500, year: 2015 },
      { month: 'September', amount: 2500, year: 2015 },
      { month: 'October', amount: 2500, year: 2015 },
      { month: 'November', amount: 2500, year: 2015 },
      { month: 'December', amount: 2500, year: 2015 },
      { month: 'March', amount: 2500, year: 2016 },
      { month: 'May', amount: 2500, year: 2016 },
      { month: 'July', amount: 2500, year: 2016 }
    ],
    name: 'Makey Johnny'
  } ];
};
&#13;
&#13;
&#13;

答案 1 :(得分:0)

由于问题标有下划线:

&#13;
&#13;
var dues = [{memberid:194,payment:[{month:'January',amount:2500,year:2015},{month:'February',amount:2500,year:2015},{month:'March',amount:2500,year:2015},{month:'April',amount:2500,year:2015},{month:'May',amount:2500,year:2015},{month:'June',amount:2500,year:2015},{month:'July',amount:2500,year:2015},{month:'August',amount:2500,year:2015},{month:'September',amount:2500,year:2015},{month:'October',amount:2500,year:2015},{month:'November',amount:2500,year:2015},{month:'December',amount:2500,year:2015},{month:'March',amount:2500,year:2016},{month:'May',amount:2500,year:2016},{month:'September',amount:2500,year:2016}],name:'Makey Trashey'},{memberid:156,payment:[{month:'January',amount:2500,year:2015},{month:'February',amount:2500,year:2015},{month:'March',amount:2500,year:2015},{month:'April',amount:2500,year:2015},{month:'May',amount:2500,year:2015},{month:'June',amount:2500,year:2015},{month:'July',amount:2500,year:2015},{month:'August',amount:2500,year:2015},{month:'September',amount:2500,year:2015},{month:'October',amount:2500,year:2015},{month:'November',amount:2500,year:2015},{month:'December',amount:2500,year:2015},{month:'March',amount:2500,year:2016},{month:'May',amount:2500,year:2016},{month:'July',amount:2500,year:2016}],name:'Makey Johnny'}];

// Get all the payments from each members & flatten it to a 1-d array
var grouped = _.chain(dues).pluck('payment').flatten().groupBy(function (payment) {
    return [payment.month, payment.year].join('_'); // group payments with month+year as the grouping key
}).map(function (monthlyPayments) {
    // Calculate total for each group
    var totalMonthlyPayment = _.reduce(monthlyPayments, function (memo, payment) {
        return payment.amount + memo;
    }, 0);
    return {month: monthlyPayments[0].month, year: monthlyPayments[0].year, total: totalMonthlyPayment};
}).value();

console.log(grouped);
&#13;
<script src="http://underscorejs.org/underscore-min.js"></script>
&#13;
&#13;
&#13;