我有以下对象数组:
[{
id: 1,
amount: 2000,
date: "2018-01-31T00:00:00.000Z"
},{
id: 2,
amount: 3000,
date: "2017-07-31T00:00:00.000Z"
},{
id: 3,
amount: 6000,
date: "2018-01-31T00:00:00.000Z"
},{
id: 4,
amount: 7000,
date: "2017-01-31T00:00:00.000Z"
},{
id: 5,
amount: 5000,
date: "2017-03-31T00:00:00.000Z"
},{
id: 6,
amount: 3000,
date: "2018-02-22T00:00:00.000Z"
},{
id: 7,
amount: 4500,
date: "2017-01-31T00:00:00.000Z"
}]
我正在调用以下命令按年份和日期对对象进行分组:
_(data)
.groupBy(o => new Date(o.date).getFullYear() + '-' + new Date(o.date).getMonth())
.map(o1 => _(o1).map(o2 => o2.amount).sum())
上面的代码给我一些总和,如[xx,yy,aaa,bbb,...]
现在我需要确保数组中的这些值将被排序(2018-2的总和将是第一个,2017-1的总和将在最后)。
如上所述,当结果将包含已排序对象的数组时也会很好,其中每个对象都将包含句点键“year-month”以检测当前值是什么。预期的输出将是这样的:
[
{period: "2018-2", amount:3000}, // sum of 2018-2
{period: "2018-1", amount: 8000 }, // sum of 2018-1
{period: "2017-7", amount: 3000} // sum of 2017-7
{period: "2017-3", amount: 5000} // sum of 2017-3
{period: "2017-1" amount: 11500} // sum of 2017-1
]
可以编辑此链以获得所需的结果吗?谢谢!
答案 0 :(得分:3)
您可以将函数reduce
与函数Object.values
Just vanilla javascript
var data = [{ id: 1, amount: 2000, date: "2018-01-31T00:00:00.000Z"},{ id: 2, amount: 3000, date: "2017-07-31T00:00:00.000Z"},{ id: 3, amount: 6000, date: "2018-01-31T00:00:00.000Z"},{ id: 4, amount: 7000, date: "2017-01-31T00:00:00.000Z"},{ id: 5, amount: 5000, date: "2017-03-31T00:00:00.000Z"},{ id: 6, amount: 3000, date: "2018-02-22T00:00:00.000Z"},{ id: 7, amount: 4500, date: "2017-01-31T00:00:00.000Z"}];
var result = Object.values(data.reduce((a, c) => {
var [year, month] = c.date.split('-');
var key = `${year}-${+month}`;
(a[key] || (a[key] = {period: key, amount: 0})).amount += c.amount;
return a;
}, {})).sort((a, b) => b.period.localeCompare(a.period));
console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)
使用lodash,您可以先groupBy
,然后对条目进行排序,然后对map
这些条目进行排序,以便为每个时段获取period
和amount
之和的对象。
const data = [{"id":1,"amount":2000,"date":"2018-01-31T00:00:00.000Z"},{"id":2,"amount":3000,"date":"2017-07-31T00:00:00.000Z"},{"id":3,"amount":6000,"date":"2018-01-31T00:00:00.000Z"},{"id":4,"amount":7000,"date":"2017-01-31T00:00:00.000Z"},{"id":5,"amount":5000,"date":"2017-03-31T00:00:00.000Z"},{"id":6,"amount":3000,"date":"2018-02-22T00:00:00.000Z"},{"id":7,"amount":4500,"date":"2017-01-31T00:00:00.000Z"}]
const result = _.chain(data)
.groupBy(({date}) => {
const d = new Date(date)
return d.getFullYear() + '-' + (d.getMonth() + 1)
})
.entries()
.sort(([a], [b]) => b.localeCompare(a))
.map(([period, arr]) => ({period, amount: _.sumBy(arr, ({amount}) => amount)}))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.js"></script>
答案 2 :(得分:1)
您可以获取分组数据并按升序排序(使用lodash的唯一可能方式)并反转订单。
var data = [{ id: 1, amount: 2000, date: "2018-01-31T00:00:00.000Z" }, { id: 2, amount: 3000, date: "2017-07-31T00:00:00.000Z" },{ id: 3, amount: 6000, date: "2018-01-31T00:00:00.000Z" }, { id: 4, amount: 7000, date: "2017-01-31T00:00:00.000Z" }, { id: 5, amount: 5000, date: "2017-03-31T00:00:00.000Z" }, { id: 6, amount: 3000, date: "2018-02-22T00:00:00.000Z" }, { id: 7, amount: 4500, date: "2017-01-31T00:00:00.000Z" }],
result = _(data)
.groupBy(o => o.date.slice(0, 7))
.map((array, sort) => ({ sort, date: sort.split('-').map(Number).join('-'), amount: _.sumBy(array, 'amount') }))
.sortBy('sort')
.reverse()
.map(({ date, amount }) => ({ date, amount }))
.value();
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>