0

I have one array,

0: (4) ['0', '0', '1', '2']
1: (4) ['0', '0', '2', '2']
2: (4) ['0', '2', '2', '2']

Tried to get sum of row and col,

   var rowSum = result.map(r => r.reduce((a, b) => a + b));

   var colSum = result.reduce((a, b) => a.map((x, i) => x + b[i])); 

but getting

  row 0012,0022,0222
     col 000,002,122,222
Vikram R
  • 776
  • 3
  • 8
  • 26

1 Answers1

0

Parse characters to integers:

const result = [
  ['0', '0', '1', '2'],
  ['0', '0', '2', '2'],
  ['0', '2', '2', '2'],
];

var rowSum = result.map(r => r.reduce((a, b) => parseInt(a) + parseInt(b)));

var colSum = result.reduce((a, b) => a.map((x, i) => parseInt(x) + parseInt(b[i])));

console.log(rowSum);
console.log(colSum);