This is the numbers array which is defined in a React Redux reducer file:
const initialState = {
numbers: [
{ name: 'max', num: 3, inCart: false },
{ name: 'jack', num: 2, inCart: true },
{ name: 'jack', num: 2, inCart: true }
]
};
In a component, I want to sum all the numeric values in all the objects in the numbers array only if inCart === true.
This is how I'm doing it:
useEffect(() => {
const total = props.numbers.reduce((prev, current) => {
if (current.inCart) return prev + current.num;
}, 0);
console.log(total);
}, []);
And this is the console log I get:
NaN
So it's not working, what am I doing wrong?
else...?