使用javascript

时间:2018-04-26 06:29:39

标签: javascript arrays typescript

我有以下数组值,

[
{
    "amount": 4.27,
    "month": 1,
    "year": 2017
},
{
    "amount": 2.46,
    "month": 1,
    "year": 2017
},
{
    "amount": 1.5,
    "month": 2,
    "year": 2017
},
{
    "amount": 28.24,
    "month": 2,
    "year": 2017
},
{
    "amount": 8.24,
    "month": 3,
    "year": 2017
},
{
    "amount": 3.65,
    "month": 3,
    "year": 2017
},
{
    "amount": 0.86,
    "month": 3,
    "year": 2017
},
{
    "amount": 0,
    "month": 1,
    "year": 2018
},
{
    "amount": 71.84,
    "month": 2,
    "year": 2018
},
{
    "amount": 26.62,
    "month": 3,
    "year": 2018
}]

必须根据特定的进行分组,最终值应采用以下格式,

[
{
    "amount": 6.63,
    "month": 1,
    "year": 2017
},
{
    "amount": 29.74,
    "month": 2,
    "year": 2017
},
{
    "amount":12.75,
    "month": 3,
    "year": 2017
},
{
    "amount": 0,
    "month": 1,
    "year": 2018
},
{
    "amount": 71.84,
    "month": 2,
    "year": 2018
},
{
    "amount": 26.62,
    "month": 3,
    "year": 2018
}]

我已经提到了这个例子:Group array items using object

但是我无法理清总结部分,所得到的只是特定月份和年份的第一个值。

3 个答案:

答案 0 :(得分:2)

使用reduceObject.valuessort

var fnGetKey = (obj) => JSON.stringify({year:obj.year, month:obj.month}); //get key from object
Object.values(arr.reduce( (a,c) => {
  var key = fnGetKey( c );
  !a[key] ? (a[key] = c) : (a[key].amount += c.amount); //Initialize with c if key doesn't exists in accumulator, else increment amount
  return a; //return accumulator
} ,{})).sort( (a,b) => a.year - b.year || a.month - b.month ); //sort by year and month

<强>演示

var arr = [{
    "amount": 4.27,
    "month": 1,
    "year": 2017
  },
  {
    "amount": 2.46,
    "month": 1,
    "year": 2017
  },
  {
    "amount": 1.5,
    "month": 2,
    "year": 2017
  },
  {
    "amount": 28.24,
    "month": 2,
    "year": 2017
  },
  {
    "amount": 8.24,
    "month": 3,
    "year": 2017
  },
  {
    "amount": 3.65,
    "month": 3,
    "year": 2017
  },
  {
    "amount": 0.86,
    "month": 3,
    "year": 2017
  },
  {
    "amount": 0,
    "month": 1,
    "year": 2018
  },
  {
    "amount": 71.84,
    "month": 2,
    "year": 2018
  },
  {
    "amount": 26.62,
    "month": 3,
    "year": 2018
  }
];

var fnGetKey = (obj) => JSON.stringify({
  year: obj.year,
  month: obj.month
}); //get key from object


var output = Object.values(arr.reduce((a, c) => {
  var key = fnGetKey(c);
  !a[key] ? (a[key] = c) : (a[key].amount += c.amount);
  return a;
}, {})).sort((a, b) => a.year - b.year || a.month - b.month);

console.log(output);

答案 1 :(得分:1)

您可以使用array#reduce根据monthyear对数据进行分组,并累积地图中对象的所有金额。然后提取所有值并对结果进行排序。

const data = [{ "amount": 4.27, "month": 1, "year": 2017 }, { "amount": 2.46, "month": 1, "year": 2017 }, { "amount": 1.5, "month": 2, "year": 2017 }, { "amount": 28.24, "month": 2, "year": 2017 }, { "amount": 8.24, "month": 3, "year": 2017 }, { "amount": 3.65, "month": 3, "year": 2017 }, { "amount": 0.86, "month": 3, "year": 2017 }, { "amount": 0, "month": 1, "year": 2018 }, { "amount": 71.84, "month": 2, "year": 2018 }, { "amount": 26.62, "month": 3, "year": 2018 } ],
    result = [...data.reduce((r,{amount, month, year}) => {
      const key = month + '/' + year;
      r.has(key) ? r.get(key).amount += amount: r.set(key, {amount, month, year});
      return r;
    },new Map).values()]
    .sort((a,b) => a.year - b.year || a.month - b.month);
console.log(result);

答案 2 :(得分:1)

对于排序数据,您可以检查最后一项并更新值或将新对象推送到结果集。

&#13;
&#13;
var array = [{ amount: 4.27, month: 1, year: 2017 }, { amount: 2.46, month: 1, year: 2017 }, { amount: 1.5, month: 2, year: 2017 }, { amount: 28.24, month: 2, year: 2017 }, { amount: 8.24, month: 3, year: 2017 }, { amount: 3.65, month: 3, year: 2017 }, { amount: 0.86, month: 3, year: 2017 }, { amount: 0, month: 1, year: 2018 }, { amount: 71.84, month: 2, year: 2018 }, { amount: 26.62, month: 3, year: 2018 }],
    grouped = array.reduce((r, { amount, month, year }) => {
        var last = r[r.length - 1];
        if (last && last.year === year && last.month === month) {
            last.amount += amount;
        } else {
            r.push({ amount, month, year });
        }
        return r;
    }, []);

console.log(grouped);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;