I have this array of objects:
[
{
"id_operation": 4,
"user_email": "[email protected]",
"concept": "Cobro de clase de ingles",
"amount": "1500",
"date": "2021-04-29T03:00:00.000Z",
"type": "egreso",
"category": 4,
"state": true
},
{
"id_operation": 5,
"user_email": "[email protected]",
"concept": "Cobro de sueldo ",
"amount": "80000",
"date": "2021-04-29T03:00:00.000Z",
"type": "egreso",
"category": 4,
"state": true
},
{
"id_operation": 7,
"user_email": "[email protected]",
"concept": "Cobro de sueldo",
"amount": "70000",
"date": "2021-04-29T03:00:00.000Z",
"type": "ingreso",
"category": 4,
"state": true
},
{
"id_operation": 9,
"user_email": "[email protected]",
"concept": "cobro de sueldo",
"amount": "70000",
"date": "2021-04-29T03:00:00.000Z",
"type": "ingreso",
"category": 4,
"state": true
},
]
what I want to do is add the "amount" by "type" (input and output). I did the following, but in console I get NaN as a result .
const [operations, setOperations] = React.useState([]);
const getOperations = async () => {
const data = await fetch("http://localhost:4000/api/operations");
const operationsData = await data.json();
setOperations(operationsData);
// aca intento sumar por type Egreso
const totalEgresos = operations.filter(item => item.type === 'egreso').reduce((count, item) => count + parseFloat(item), 0)
console.log('Total: ' + totalEgresos);
}
You are passing the entire object when
parseFloat
you need to pass the propertyitem.amount