Reduce multiple matrixes to single matrix and perform value addition
Reduce multiple matrixes to single matrix and perform value addition
I have a list (array) of matrixes in a data structure like so:
const data = [
matrix:
rows:
ROW1:
cols:
COL1: "4",
COL2: "2"
,
ROW2:
cols:
COL1: "1",
COL2: "4"
,
ROW3:
cols:
COL1: "2",
COL2: "1"
,
,
matrix:
rows:
ROW1:
cols:
COL1: "1",
COL2: "6"
,
ROW2:
cols:
COL1: "2",
COL2: "3"
,
ROW3:
cols:
COL1: "5",
COL2: "2"
];
I would like to sum up the values defined at the same ROW:COL pairs and end up with a single matrix like so:
const newMatrix =
rows:
ROW1:
cols:
COL1: "5",
COL2: "8"
,
ROW2:
cols:
COL1: "3",
COL2: "7"
,
ROW3:
cols:
COL1: "7",
COL2: "3"
;
I do not know beforehand how many matrixes I might receive in the data array. I also do not know beforehand how many rows and columns I receive.
I started off by calling reduce() on the data but got stuck because I cannot think of a way of finding and then summing up the data at ROW:COL pairs of the same name.
const newMatrix = data.reduce(( accumulator, current ) =>
let newMatrix;
// ...???
// find? forEach? Object.entries? for loops? maps inside maps?
// Everything I tried failed
return newMatrix;
);
I have also messed around with forEach(), Object.values(), find() and all sorts of for loops inside other loops but I keep ending up in some kind of brain loop myself.
I realise this might not be enough to work yet but I'm new at programming and don't know where to go next.
I have been stuck for 2.5 days now on this very problem so even if no straight up solution is offered I would appreciate any tips or guidance in the right direction.
Unfortunately I have no peers who I can ask for help.
Thank you.
@samabcde No, my bad. Structure should be consistent. Corrected now.
– noob
Sep 15 '18 at 18:04
Still find syntax error on the script you provided, please check if the structure is correct.
– samabcde
Sep 16 '18 at 4:26
@samabcde It appears in the data array I closed it using } instead of ], this is now also corrected.
– noob
Sep 16 '18 at 7:27
rows only contains ROW1, is it correct? rows: ROW1: cols: COL1: "5", COL2: "8" , ROW2: cols: COL1: "3", COL2: "7" ,
– samabcde
Sep 16 '18 at 7:29
3 Answers
3
Since the number of row and column is not static, a recursive function will be applied to get the sum without assuming column or row property name.
const data = [
matrix:
rows:
ROW1:
cols:
COL1: "4",
COL2: "2"
,
ROW2:
cols:
COL1: "1",
COL2: "4"
,
ROW3:
cols:
COL1: "2",
COL2: "1"
,
,
matrix:
rows:
ROW1:
cols:
COL1: "1",
COL2: "6"
,
ROW2:
cols:
COL1: "2",
COL2: "3"
,
ROW3:
cols:
COL1: "5",
COL2: "2"
];
var result = ;
var sum = function(from, to)
var keys = Object.keys(from);
for (let i = 0; i < keys.length; i++)
if (to[keys[i]] === undefined)
to[keys[i]] = from[keys[i]];
else
if (typeof from[keys[i]] === "object")
sum(from[keys[i]], to[keys[i]])
else
to[keys[i]] = parseInt(from[keys[i]]) + parseInt(to[keys[i]]);
;
for (index in data)
sum(data[index], result);
console.log(result);
You have to do it in reverse, meaning loop for columns, then rows, then matrices. Following code has detailed solution.
function sumMatrices(data)
const data = [
matrix:
rows:
ROW1:
cols:
COL1: "4",
COL2: "2"
,
ROW2:
cols:
COL1: "1",
COL2: "4"
,
ROW3:
cols:
COL1: "2",
COL2: "1"
,
,
matrix:
rows:
ROW1:
cols:
COL1: "1",
COL2: "6"
,
ROW2:
cols:
COL1: "2",
COL2: "3"
,
ROW3:
cols:
COL1: "5",
COL2: "2"
];
sumMatrices(data);
You can try looping through the keys of your object and build your final result adding the values as you go.
const data = [
matrix:
rows:
ROW1:
cols:
COL1: "4",
COL2: "2"
,
ROW2:
cols:
COL1: "1",
COL2: "4"
,
ROW3:
cols:
COL1: "2",
COL2: "1"
,
,
matrix:
rows:
ROW1:
cols:
COL1: "1",
COL2: "6"
,
ROW2:
cols:
COL1: "2",
COL2: "3"
,
ROW3:
cols:
COL1: "5",
COL2: "2"
];
const sumMatrices = (array) =>
const summed = matrix: rows: ;
array.forEach((item) =>
Object.keys(item.matrix.rows).forEach((row) =>
if (!summed.matrix.rows[row]) summed.matrix.rows[row] = cols: ;
Object.keys(item.matrix.rows[row].cols).forEach((col) =>
if (!summed.matrix.rows[row].cols[col]) summed.matrix.rows[row].cols[col] = 0;
summed.matrix.rows[row].cols[col] += +item.matrix.rows[row].cols[col];
);
);
);
return summed;
;
console.log(sumMatrices(data));
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
Is it intended for ROW3 has different structure(not surrounded by ) with ROW1 and ROW2?
– samabcde
Sep 15 '18 at 15:08