var json = JSON.parse(`{
"response": {
"items": [
{
"id": "898",
"date": "18 января, 2021",
"time": "22:38",
"account": "%account%",
"category": "%category%",
"amount": "1000.00",
"amount_html": "%html%",
"type": "1",
"type_html": "%type_html%",
"balance": "%balance%",
"comment": "-"
},
{
"id": "898",
"date": "19 января, 2021",
"time": "22:38",
"account": "%account%",
"category": "%category%",
"amount": "1000.00",
"amount_html": "%html%",
"type": "1",
"type_html": "%type_html%",
"balance": "%balance%",
"comment": "-"
},
{
"id": "898",
"date": "19 января, 2021",
"time": "22:38",
"account": "%account%",
"category": "%category%",
"amount": "1000.00",
"amount_html": "%html%",
"type": "1",
"type_html": "%type_html%",
"balance": "%balance%",
"comment": "-"
},
{
"id": "898",
"date": "19 января, 2021",
"time": "22:38",
"account": "%account%",
"category": "%category%",
"amount": "1000.00",
"amount_html": "%html%",
"type": "1",
"type_html": "%type_html%",
"balance": "%balance%",
"comment": "-"
},
{
"id": "898",
"date": "20 января, 2021",
"time": "22:38",
"account": "%account%",
"category": "%category%",
"amount": "1000.00",
"amount_html": "%html%",
"type": "1",
"type_html": "%type_html%",
"balance": "%balance%",
"comment": "-"
},
{
"id": "898",
"date": "21 января, 2021",
"time": "22:38",
"account": "%account%",
"category": "%category%",
"amount": "1000.00",
"amount_html": "%html%",
"type": "1",
"type_html": "%type_html%",
"balance": "%balance%",
"comment": "-"
},
{
"id": "898",
"date": "21 января, 2021",
"time": "22:38",
"account": "%account%",
"category": "%category%",
"amount": "1000.00",
"amount_html": "%html%",
"type": "1",
"type_html": "%type_html%",
"balance": "%balance%",
"comment": "-"
}
]
}
}`);
const accountsGroupedByDates = json.response.items
.reduce((acc, item) => {
if (Array.isArray(acc[item.date])) {
acc[item.date].push(item)
} else {
acc[item.date] = [item];
}
return acc;
}, {})
const listBalances = Object.entries(accountsGroupedByDates)
.map(([date, accountsList]) => {
return {
date,
accounts: accountsList,
dailyBalance: accountsList.reduce((acc, item) => {
const amount = Number(item.amount);
return item.type === '1' ? acc + amount : acc - amount;
}, 0)
}
});
$('div.timeline').append(listBalances.map(item => {
let div = `<div class="time-label"><span class="bg-primary">${item.date} - ${item.dailyBalance}</span></div>`
item.accounts.forEach(_item => {
div += ` <div>
${_item.type_html}
<div class="timeline-item">
<span class="float-right" style="padding: 5px 10px 5px 5px;">${_item.amount_html}</span>
<h3 class="timeline-header">${_item.account}</h3>
<div class="timeline-body"><small class="float-right">${_item.time}</small><b>${_item.category}</b><br>${_item.comment}</div>
</div>
</div>`
})
return div;
}).join(''));