Untitled
unknown
javascript
4 years ago
1.4 kB
3
Indexable
const JSONdata = require('./bank-data.json'); const data = JSONdata.Worksheet.Table.Row; let spendingMap = new Map(); let incomeMap = new Map(); let spendings = []; let incomes = []; data.forEach(row => { const transaction = { date: row.Cell[0].Data["#text"], name: row.Cell[1].Data["#text"], amount: Number(row.Cell[2].Data["#text"]), } if (transaction.amount < 0) { spendings.push(transaction); //add to map if not already there, otherwise add to existing value if (spendingMap.has(transaction.name)) { spendingMap.set(transaction.name, spendingMap.get(transaction.name) + transaction.amount); } else { spendingMap.set(transaction.name, transaction.amount); } } else { incomes.push(transaction); //add to map if not already there, otherwise add to existing value if (incomeMap.has(transaction.name)) { incomeMap.set(transaction.name, incomeMap.get(transaction.name) + transaction.amount); } else { incomeMap.set(transaction.name, transaction.amount); } } }); spendings.sort((a, b) => { return a.amount - b.amount; }); incomes.sort((a, b) => { return a.amount - b.amount; }); // sort the map by value let sortedMap = new Map([...spendingMap.entries()].sort((a, b) => b[1] - a[1])); console.log(spendings.reduce((a, b) => a + b.amount, 0));
Editor is loading...