I have an array like this having data as number values. I have to find out the sum of column values.
const data = [
[{'data':0},{'data':3},{'data':1},{'data':0}],
[{'data':1},{'data':1},{'data':1},{'data':1}],
[{'data':2},{'data':2},{'data':2},{'data':2}],
[{'data':3},{'data':3},{'data':3},{'data':3}]
];
I have to calculate the sum of records at 0 index together, 1 index together and 2 index together. Result should be calculate on the basis of column values sum.
My approach on this but it is not working alright. Giving me the different results. What is the wrong approach I am doing here.Also, I was getting result[i] as undefined so I put it in a condition with initialize empty array. But, it doesn't seems to be right logic to me.
for( let i = 0; i < data.length; i++ ) {
let sum = 0;
for( let j = 0; j < data[i].length; j++ ) {
if( !result[i] ){
result[i] = [];
}
sum = sum+data[i][j].data;
result[i][j] = data[i][j];
index++;
}
}
Expected result on the basis of column values sum. e.g. first colum values are 0, 1,2,3 the sum will be 6.
let expectedResult = [6,9,8,6]