How to calculate correctly

I try to calculate votes from my Dapp.
But I dont know how to calculate them this is what i have so fare

    useEffect(() => {
    const fetchAllVotes = async () => {
        const items = await fetchItems(); // get all the items
        const votesPerItem = await Promise.all(
            items.map(async (item) => {
                const votes = await getVotes(item.id); // get all the votes for the item
                return votes;
            })
        );
        const allVotes = votesPerItem.flat(); // combine all the votes into a single array
        setVotes(allVotes);
    };
    fetchAllVotes();
}, [])


votes.map((vote) => {
    console.log(vote.toString());
})

this is my console but how do i calculate the numbers

I also tried to reduce like this.. But then i get the result like the second picture indicates

  const totalVotes = votes.reduce((accumulator, currentValue) => accumulator + currentValue, 0);

console.log("Total votes:", totalVotes);

Then i get this

>Solution :

Looks like you are getting an array of string. Don’t forget to convert them into integer or float:

const sum = votes.reduce((partialSum, a) => parseInt(partialSum) + parseInt(a), 0);

Leave a Reply