Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // before
- let objArr = [
- {
- date: '01/01/2018'
- total: 1
- },
- {
- date: '01/01/2018'
- total: 2
- },
- {
- date: '01/02/2018'
- total: 3
- },
- {
- date: '01/02/2018'
- total: 4
- },
- ...
- ]
- // final result
- let finalArr = [
- {
- date: '01/01/2018'
- total: 3
- },
- {
- date: '01/02/2018'
- total: 7
- },
- ...
- ]
- objArr.reduce((acc, obj) => {
- acc.set(obj.date, (acc.get([obj.date]) || 0) + obj.total);
- return acc;
- }, new Map())
- // bad output
- badArray = [
- ...,
- {
- date: '01/02/2018'
- total: 4
- },
- {
- date: undefined
- total: NaN
- },
- {
- date: undefined
- total: NaN
- }
- ]
Add Comment
Please, Sign In to add comment