M
M
Marat Ivanov2020-08-24 09:32:03
JavaScript
Marat Ivanov, 2020-08-24 09:32:03

How to calculate sum of cells in js table columns, react?

there is a component that receives data from the server in the state I hold strings

state = {
    row: ,}
then I run map and load it into a table. you need to display the sum of the cells in each column.
row.map((element, i) => (
                    <tr key={i} className="tableRow">
                      {element.map((item, j) => (
                        <td key={j}>
                          {
                            <input
                              className="input-edit"
                              value={item}
                              placeholder="-"
                              onChange={(e) => {
                                this.handleChangeInput(i, j, e);
                              }}
                            />
                          }
                        </td>
                      ))

Please advise how to implement this.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WapSter, 2020-08-24
@wapster92

const obj = {
  rows: [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ]
}

const getSumColumn = (arr, column) => {
  let sum = 0
  arr.forEach(el => sum += el[column])
  return sum
}

console.log(getSumColumn(obj.rows, 1)) // 15

Xs why there are lines, and xs how you store data there. But in general, it sucks not to know js and use react

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question