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)));