0

I have a two-dimensional array, the cells are an object {id, amount}, you need to add a sum of columns, while using only methods. What I did -

let matrix = [
    [{id:1, amount:11},{id:2, amount:22},{id:3, amount:33}],
    [{id:4, amount:44},{id:5, amount:55},{id:6, amount:66}],
    [{id:7, amount:77},{id:8, amount:88},{id:9, amount:99}],
    [{id:10, amount:100},{id:11, amount:111},{id:12, amount:112}],
];

let c = matrix.reduce((acc, cur)=> {
    return acc.map((item, index)=> {
        return item + cur[index].amount;
    })
});
console.log(c);
3
  • Please add the expected output. Commented Jul 26, 2019 at 14:38
  • Yup, it's actually quite unclear! I think everyone didn't understand the same thing. Commented Jul 26, 2019 at 14:42
  • output value: ["[object Object]4477100", "[object Object]5588111", "[object Object]6699112"] but need: [232, 276, 310] Commented Jul 29, 2019 at 8:17

4 Answers 4

2

Why not using flatMap then reduce?

let matrix = [
        [{id:1, amount:11},{id:2, amount:22},{id:3, amount:33}],
        [{id:4, amount:44},{id:5, amount:55},{id:6, amount:66}],
        [{id:7, amount:77},{id:8, amount:88},{id:9, amount:99}],
        [{id:10, amount:100},{id:11, amount:111},{id:12, amount:112}],
    ];
let result = matrix.flatMap(it => it).reduce((acc, item) => acc + item.amount, 0);
console.log(result)

EDIT

After getting what you actually wanted to do, here is a complement (feel free not to read above).

const matrix = [
        [{id:1, amount:11},{id:2, amount:22},{id:3, amount:33}],
        [{id:4, amount:44},{id:5, amount:55},{id:6, amount:66}],
        [{id:7, amount:77},{id:8, amount:88},{id:9, amount:99}],
        [{id:10, amount:100},{id:11, amount:111},{id:12, amount:112}],
    ];
    
let result = matrix
             .flatMap(it => it) //This flattens the items.
             .reduce((acc, item, i) => (acc[i%matrix[0].length] += item.amount, acc), //This computes the sum based on the indices and the matrix width.
             Array.apply(0, Array(matrix[0].length)).map(_ => 0)); //This inits the result array with zeros.

console.log(result)

Sign up to request clarification or add additional context in comments.

2 Comments

I need an array of each column amount, example [232, 276, 310]
@ЖеняБорісевіч another piece of code ;) You tell me if you like it!
0

You can create an array of length equal to width of array. Then use nested forEach to add the values to corresponding element.

let matrix = [
        [{id:1, amount:11},{id:2, amount:22},{id:3, amount:33}],
        [{id:4, amount:44},{id:5, amount:55},{id:6, amount:66}],
        [{id:7, amount:77},{id:8, amount:88},{id:9, amount:99}],
        [{id:10, amount:100},{id:11, amount:111},{id:12, amount:112}],
    ];

let res = Array(matrix[0].length).fill(0)
matrix.forEach(x => {
  x.forEach((y,i) => {
    res[i] += y.amount;
  })
})

console.log(res)

1 Comment

possible without using for, foreach
0

So decided, if there are better solutions, I will be glad to see

getColSumMatrix = matrix =>
    matrix.reduce((acc, cur) =>
      acc.map((value, i) =>
        typeof value == "object"
          ? value.amount + cur[i].amount
          : value + cur[i].amount
      )
    );

Comments

-1
const summ = matrix.reduce((acc, table) => {
        return (
            acc +
            table.reduce((acc, { amount }) => {
                return acc + amount;
            }, 0)
        );
    }, 0);

1 Comment

I need an array of each column amount, example [232, 276, 310]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.