function getSum(total, num) {
return (total + parseInt(num.equipment));
}
useEffect(() => {
var firestoreArray = [...]; //this is populated with the array of objects
// from firestore when you fetch. format is {name: "shovel", equipment: "12" }
var total = firestoreArray.reduce(getSum, 0); //reduce function where the 0 is the starting
//point of the sum. the getSum function is adding by parsing to Integer first for all the
//objects "equipment" attribute.
}, [firestoreArray]}
1条答案
按热度按时间lrl1mhuk1#
我认为这是一个纯粹的JS问题,我的解决方案是在从firestore中获取对象后,对对象数组使用JS reduce函数。