This is my array which I’m pulling from Firebase with a listener.
[{"Amount": "40", "Day": "08/31/2022"}, {"Amount": "300", "Day": "09/01/2022"}, {"Amount": "1715", "Day": "08/31/2022"}, {"Amount": "250", "Day": "08/31/2022"}, {"Amount": "100", "Day": "08/31/2022"}, {"Amount": "200", "Day": "08/31/2022"}]
I want this array to sum up all values on the same day, and return just one with the summed amount. So I can use it for a chart in React Native with victory native.
This is my code, which is working fine, but I’ve been looking and looking all over the internet on how, to sum up, but no luck so far!
const q = query(collection(db, "users"), where("moneda", "==", "$"));
const unsubscribe = onSnapshot(q,(querySnapshot) => {
const dolarCurrency = [];
const months =[];
querySnapshot.forEach((doc) =>{
dolarCurrency.push(doc.data().cantidad);
months.push(doc.data().fecha)
})
const hash = months.map((Day) => ({ Day }));
const hashDolar = dolarCurrency.map(( Amount ) => ({ Amount }))
const output = hash.map(({Day},i) => ({Day, ...hashDolar[i]}));
console.log(output)
})
Any help or advice will be much appreciated my friends!
2
Answers
Object.values
to get those objects into a new array.Reduce array function can solve your problem.