'array of objects filter and sum by similar numbers
I have this array of object and I need to get the sum of the amount column that has number greater than 3 and the same first 3 digits.
The object is :
Acc Number Amount
1 -5425
10 12
100 100
1000 -4971
1001 84000
1002 500
1003 6411
101 654
1010 245
102 288
1020 21
1021 68
1020 -87
1023 -668
The resulting object should be like below by suming all the 4 digits acc number after each similar 3 digits acc number e.g. sum of acc number 1000,1001,1002,1003(under acc number 100) gives 85,940
[
{
desc: "1",
Amount: 5424
}
{
desc: "10",
Amount: 12
}
{
desc: "100",
Amount: 100
}
{
desc: "1000 ",
Amount: -4971
}
{
desc: "1001 ",
Amount: 84000
}
{
desc: "1002 ",
Amount: 500
}
{
desc: "1005 ",
Amount: 6411
}
{
desc: "Sum account 100 with 4 digits",
Amount: 85940
}
{
desc: "1010",
Amount: 245
}
{
desc: "Sum account 101 with 4 digits,
Amount: 245
}
{
desc: "102",
Amount: 288
}
{
desc: "1020",
Amount: 21
}
{
desc: "1021 ",
Amount: 68
}
{
desc: "1022",
Amount: -87
}
{
desc: "1023 ",
Amount: -668
}
{
desc: "Sum account 102 with 4 digits ",
Amount: -666
}
]
I have tried this code but it's not working:
const filtered = newaccount.filter(obj => obj.number.length >3 &&
obj.number?.includes(obj.number.slice(0, 3)));
Solution 1:[1]
I'm not sure I completely understand your question so I'm going to paraphrase and please correct me if I'm wrong: You have an array of objects, { number: number, amount: number }, and you want to sum the amounts of objects sharing the same first 3 digits in the number field where the length of number is greater than 3 (in other words, account number over 99):
interface AccountBalance {
number: number,
amount: number
}
interface AccountSumDescription {
desc: string,
sum: number
}
const balances: AccountBalance[] = [
{number:1, amount: -5425},
{number:10, amount: 12},
{number:100, amount: 100},
{number:1000, amount: -4971},
{number:1001, amount: 84000},
{number:1002, amount: 500},
{number:1002, amount: 6411},
{number:1010, amount: 245},
{number:102, amount: 288},
{number:1020, amount: 21},
{number:1021, amount: 68},
{number:1020, amount: -87},
{number:1032, amount: -668}
];
function sumBalancesForAccountNumbersOver99(balances: AccountBalance[]): AccountSumDescription[] {
const accountSums: Map<string, number> = new Map<string, number>();
balances.filter(b => b.number > 99).forEach(b => {
const key: string = b.number.toString().slice(0,3);
const accountSum = accountSums.get(key);
accountSums.set(key, (accountSum !== undefined ? accountSum : 0) + b.amount);
});
const result: AccountSumDescription[] = [];
accountSums.forEach((v,k) => result.push({desc: `Sum accounts ${k}*`, sum: v}));
return result;
}
console.log(sumBalancesForAccountNumbersOver99(balances));
The output should be:
[
{ desc: 'Sum accounts 100*', sum: 86040 },
{ desc: 'Sum accounts 101*', sum: 245 },
{ desc: 'Sum accounts 102*', sum: 290 },
{ desc: 'Sum accounts 103*', sum: -668 }
]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Joshybull |
