I have an array stating salary of different sectors. I need to calculate and then create a table based on this . I’m really confused how to do it . Here is an example data
const data=[
['Euro','Tech'],
['USD','Tech'],
['GBX', 'Health'],
['Euro', 'Real Estate'],
['GBX', 'Real Estate'].
]
Now I have to display the sector ,salary & total in the Table like this below:
Sector | Euro | GBX | USD | Total |
---|---|---|---|---|
Tech | 1 | 0 | 1 | 2 |
Health | 0 | 1 | 0 | 1 |
Real Estate | 1 | 1 | 0 | 2 |
Total | 2 | 2 | 1 | 5 |
Could you help me with this. I’m using React table to display the data.
2
Answers
try this code to format data array
result will be like this
Here the idea is to create a two dimensional array filled with zeros, then work through each item in the data array and increment the correct value in the two diminsional array based on a lookup using a map of sector names to indexes and a map of currency names to indexes.
Hopefully it helps, though I feel like there’s probably a better way to do it.