I have a response json like this:
[
{
"DESCRIPTION": "OER",
"OER_HI": 23.23650286618332,
"OER_BI": 23.419567144988942,
"OER_SBI": 23.74404977161088
},
{
"DESCRIPTION": "CPO Production",
"OER_HI": 1046.17,
"OER_BI": 10538.517,
"OER_SBI": 87788.887
},
{
"DESCRIPTION": "CPO Dispatch",
"OER_HI": 1489.6000000000001,
"OER_BI": 9145.67,
"OER_SBI": 81187.22
},
{
"DESCRIPTION": "CPO Stock",
"OER_HI": 12032.43,
"OER_BI": null,
"OER_SBI": null
},
{
"DESCRIPTION": "Total Oil, Losses/FFB",
"OER_HI": 1.4761952,
"OER_BI": 1.4469707631313098,
"OER_SBI": 1.44239404903668
}
]
And I want to map it so it will fill the table header with the json keys and rows with the json values like this:
This screenshot is how it is supposed to look, so I mapped it using this code:
import React, {useEffect} from "react";
import {connect, useDispatch} from "react-redux";
import {Table, Header} from "semantic-ui-react";
import {fetchChart8} from "../redux/actions";
import LoadingStatus from "./templates/LoadingStatus";
import _ from "lodash"
const TableChart = ({chart_8}) => {
const dispatch = useDispatch();
let testMap = chart_8?.map(data => {
return Object.keys(data)
})
let description = testMap[0]
let oer_hi = testMap[0]
let oer_bi = testMap[0]
let oer_sbi = testMap[0]
useEffect(() => {
dispatch(fetchChart8())
}, [dispatch]);
if (_.isNull(chart_8)) return <LoadingStatus/>;
return (
<div>
<Header>
<Header.Content>CPO</Header.Content>
</Header>
<Table celled>
<Table.Header>
<Table.Row>
<Table.HeaderCell>
{description}
</Table.HeaderCell>
<Table.HeaderCell>
{oer_hi}
</Table.HeaderCell>
<Table.HeaderCell>
{oer_bi}
</Table.HeaderCell>
<Table.HeaderCell>
{oer_sbi}
</Table.HeaderCell>
</Table.Row>
</Table.Header>
<Table.Body>
{chart_8.map((data) => {
return (
<Table.Row>
<Table.Cell>
{data["DESCRIPTION"]}
</Table.Cell>
<Table.Cell>
{data["OER_HI"]}
</Table.Cell>
<Table.Cell>
{data["OER_BI"]}
</Table.Cell>
<Table.Cell>
{data["OER_SBI"]}
</Table.Cell>
</Table.Row>
)
})}
</Table.Body>
</Table>
</div>
);
};
const mapStateToProps = (state) => {
return {
chart_8: state.dashboard.chart_8
};
};
export default connect(mapStateToProps, { fetchChart8 })(TableChart);
Instead I got a table like this, with table headers filled with duplicate values of json keys. What did I do wrong in the code shown?


