Js, two-dimensional array statistics

var obj = [
            { a: 1, b: 2, c: 3 },
            { a: 4, b: 5, c: 6 },
            { a: 7, b: 8, c: 9 },
            { a: 2, b: 3, c: 10 }
        ];

clipboard.png
the total block is added from the bottom, that is, first, sum4=a4+b4+c5=2+3+10=15, is the last in the total, and the second to last is sum3=sum4+c3=15+9=24;, the second is sum2=sum3+c2=24+6=30;, the first is sum1=sum2+c1=30+3=33;, what should I say?

Sep.30,2021

obj.reduceRight((iter, val, idx) => {
    if(idx === obj.length - 1) {
        val.sum = val.a + val.b + val.c
    } else {
        val.sum = val.c + iter[0].sum
    }
    iter.unshift(val)
    return iter
}, [])

ps: can't find a better way than upstairs.

let result = 0;

obj.reverse();
obj.forEach((item, index) => {
    if (index === 0) {
        result += item.a + item.b + item.c;
    } else {
        result += item.c;
    }
    item.result = result;
});
obj.reverse();

console.log(obj);

output
[{a: 1, b: 2, c: 3, result: 33},
{a: 4, b: 5, c: 6, result: 30},
{a: 7, b: 8, c: 9, result: 24},
{a: 2, b: 3, c: 10, result: 15}]

Menu