【发布时间】:2022-01-10 17:41:51
【问题描述】:
我得到的是一个包含类别、日期和值的对象数组,就像这样
const records =
[ { category: 'Cat 1', date: '2020-11-03', value: '300.00' }
, { category: 'Cat 2', date: '2020-11-03', value: '350.00' }
, { category: 'Cat 3', date: '2020-11-03', value: '50.00' }
, { category: 'Cat 1', date: '2020-11-13', value: '200.00' }
, { category: 'Cat 1', date: '2020-12-23', value: '100.00' }
, { category: 'Cat 2', date: '2020-11-23', value: '350.00' }
, { category: 'Cat 3', date: '2020-11-15', value: '50.00' }
, { category: 'Cat 3', date: '2021-01-15', value: '50.00' }
]
我想要得到的是每个类别、月份和每月总价值的新数组,像这样:
const newRecords =
[ { category: 'Cat 1'
, totalPerMonth:
[ { month: '2020-11', totalMonthlyValue: '500.00' }
, { month: '2020-12', totalMonthlyValue: '100.00' }
] }
, { category: 'Cat 2'
, totalPerMonth:
[ { month: '2020-11', totalMonthlyValue: '700.00' }
] }
, { category: 'Cat 3'
, totalPerMonth:
[ { month: '2020-11', totalMonthlyValue: '100.00' }
, { month: '2021-01', totalMonthlyValue: '50.00' }
] } ]
【问题讨论】:
-
如果 newRecord 是一个以 'Cat 1','Cat 2' 为字段的对象会不会更容易
标签: javascript arrays sorting filtering