I have some array of objects and I need to calculate the total value of each row and total values of each column and finally I will add new key to each object using Javascript. I am explaining my code below.
my input is like below.
let book_data= [
{ BranchName: 'Deeksha, Thanisandra, Bengaluru',
Uniform: 2,
Shoes: 1,
Accessories: 1,
Books: 5,
},
{ BranchName: 'Deeksha, Thanisandra, Bengaluru',
Uniform: 2,
Shoes: 3,
Accessories: 4,
Books: 5,
}
]
Here I need to add one more column i.e-Grand total to each row and this should contain the sum of all key value rather than BranchName. Similarly again I need to append one more new record as last row and it should contant total value of each column. I am giving my sample output below.
I am explaining my code below.
for(let i=0; i<book_data.length; i++) {
let grandRowTotal = 0;
for(let key in book_data[i]) {
if(key !== 'BranchName') {
console.log('key', book_data[i][key]);
grandRowTotal += parseInt(book_data[i][key]);
}
}
book_data[i]['Grand Total'] = grandRowTotal;
console.log('grandRowTotal', book_data[i],book_data);
}
As per my code its not giving the expected result. I need here the sum of each row and column value as per my sample output above.
