I want to display sum of pharmacy and sum of client number for each region in the table.
In each array object I have pharmacy and client data. I want to group them as per the region and Region should be the column header.
I grouped the json object based on the region.
I not able to sum the data in the table.
I am getting first value only in the each column not the sum of table.
allstates.json:
[
{
"id": "AL",
"val": "01",
"bgcolor": "green",
"pharmacy": 10,
"client": 20,
"region": "Northeast"
},
{
"id": "AR",
"val": "05",
"bgcolor": "green",
"pharmacy": 10,
"client": 12,
"region": "Northeast"
},
{
"id": "DE",
"val": "10",
"bgcolor": "red",
"pharmacy": 10,
"client": 20,
"region": "MidAtlantic"
},
{
"id": "FL",
"val": "12",
"bgcolor": "red",
"pharmacy": 10,
"client": 20,
"region": "MidAtlantic"
},
{
"id": "GU",
"val": "66",
"bgcolor": "red",
"pharmacy": 30,
"client": 20,
"region": "Southease"
},
{
"id": "HI",
"val": "15",
"bgcolor": "red",
"pharmacy": 10,
"client": 20,
"region": "Southease"
},
{
"id": "ID",
"val": "16",
"bgcolor": "red",
"pharmacy": 10,
"client": 20,
"region": "Southease"
},
]
summary.js:
import React from "react";
import allStates from "./data/allstates.json";
import groupBy from 'lodash/groupBy';
import './App.css'
const Summary = () => {
let grouppedArray = groupBy(allStates, 'region')
return (
<table class="table">
<thead>
<tr>
{Object.keys(grouppedArray).map(key => {
return (
<th>{key}</th>
)
})}
</tr>
</thead>
<tbody>
<tr>
{Object.values(grouppedArray).map(values => {
console.log(values, "values")
var pharmacyTotal = 0;
var clientTotal = 0;
for (let i = 0; i < values.length; i++) {
pharmacyTotal += values[i].pharmacy;
console.log(values[i].pharmacy, "test")
pharmacyTotal = values[i].pharmacy + i;
clientTotal += values[i].client;
return (
<td>
<p>Pharmacy:{pharmacyTotal}</p>
<p>Client: {clientTotal}</p>
</td>
)
}
})}
</tr>
</tbody>
</table>
);
};
export default Summary;