Let's say I have below data
const data = [
{
id: 1,
name: "Tim",
score: [
{
score: 4,
type: "communication"
},
{
score: 4,
type: "delivery"
}
]
},
{
id: 2,
name: "Ken",
score: [
{
score: 2,
type: "communication"
},
{
score: 4,
type: "delivery"
}
]
}
];
and below object variable at the beginning
let typeTotalScore = {
communication: 0,
delivery: 0
};
and useState hook
const [scoreByType, setScoreByType] = useState({});
I want to loop through data and get below result and then use setScoreByType(typeTotalScore).
{
communication: 6,
delivery: 8
}
How should I do it in useEffect
useEffect(() => {
async function fetchData() {
let typeTotalScore = {
communication: 0,
delivery: 0
};
// some looping?
setScoreByType(typeTotalScore)
}
fetchData();
}, []);