I want to obtain the sum of total of votes using reduce()
, but I am always receiving NaN
as a result, instead of 20.
I have the following js code:
const choiseArray=[
{choice: "Shooter",url:"/questions/10/choices/01",votes:3},
{choice: "Shooter2",url:"/questions/10/choices/02",votes:2},
{choice: "Shooter3",url:"/questions/10/choices/03",votes:4},
{choice: "Shooter3",url:"/questions/10/choices/03",votes:11},
];
const reducer = (accumulator, currentValue) => parseInt(accumulator.votes) + parseInt(currentValue.votes);
const totalVotes= choiseArray.reduce(reducer);
console.info(totalVotes);
Could you tell me what I am doing wrong and why?