0

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 2

1

Reduce array function can solve your problem.

const output = [{"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"}]

const sum = output.reduce((acc, cur)=> {
    const found = acc.find(val => val.Day === cur.Day)
    if(found){
        found.Amount+=Number(cur.Amount)
    }
    else{
        acc.push({...cur, Amount: Number(cur.Amount)})
    }
    return acc
}, [])

console.log(sum)
Sign up to request clarification or add additional context in comments.

2 Comments

How can I access the local sum variable from a global function? I want to implement it in a return in React Native
You can use UseState and props. Pass a set parameter to the component. For example, props.setSum(sum). Alternatively, React has solutions such as UseContext or Redux.
0
  1. Create a new object that can store key/value pairs where the key is the date.
  2. Iterate over the objects adding the date as the key if it doesn't exist, and initialising it to an object with a date, and an amount set to zero.
  3. Add the current amount value to the amount store in the object for that date.
  4. Finally, after the loop is complete, use Object.values to get those objects into a new array.

const arr = [{"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"}];

const out = {};

for (const { Amount, Day } of arr) {
  out[Day] ??= { Day, Amount: 0 };
  const subtotal = out[Day].Amount + Number(Amount);
  out[Day] = { ...out[Day], Amount: subtotal };
}

console.log(Object.values(out));

2 Comments

Hi Andy, I got this back : [SyntaxError: Unexpected token '?']
Hm. Your environment seems to have an issue with the logical nullish assignment. out[Day] = out[Day] || { Day, Amount: 0 }; would work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.