Answer the question
In order to leave comments, you need to log in
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>
))
Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question