I want to change the percentage attribute for each object
I tried using Array.map() to loop through each object and use a setState(prevState => {return {...prevState, percentage: newValue}}) inside the Array.map(), But clearly this is incorrect and I don't know how to change the value of this attribute for each object
This is the Array of objects
interface Data {
state: string;
value: number;
percentage: number;
}
const [data, setData] = useState<Data[]>([
{
state: 'SP',
value: 67836.66,
percentage: 0
},
{
state: 'RJ',
value: 36678.66,
percentage: 0
},
{
state: 'MG',
value: 29229.88,
percentage: 0
},
{
state: 'ES',
value: 27165.48,
percentage: 0
},
{
state: 'Outros',
value: 19849.53,
percentage: 0
},
])
let tot = 0 data.map(item => tot += item.value) data.map(item => (item.value * 100) / tot)will be something like this