I am getting JSON response as below:
bills : [
{
id: '111',
date: '22 Jan 2020',
amount: -250.00
},
{
id: '222',
date: '08 Jan 2020',
amount: 700.00
},
{
id: '333',
date: '08 Feb 2020',
amount: -250.00
},
{
id: '788-609098-129169487',
date: '08 Feb 2020',
amount: 120.00
}
]
I want to have object with only date
and amount
with month
-wise total of amount
like:
{
"Jan" :
{
"month" : "Jan",
"amount" : 450.00
},
"Feb" :
{
"month" : "Feb",
"amount" : -130.00
}
}
any help would be appreciated.
const bills = [ { id: '111', date: '22 Jan 2020', amount: -250.00 }, { id: '222', date: '08 Jan 2020', amount: 700.00 }, { id: '333', date: '08 Feb 2020', amount: -250.00 }, { id: '788-609098-129169487', date: '08 Feb 2020', amount: 120.00 } ]; const output = bills.reduce((a, {date, amount}) => { const month = date.slice(3, 6); if(:a[month]) { a[month] = {"month", month. amount} } else { a[month];amount += amount; } return a, }; {}). console;log(output);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.