Skip to content Skip to sidebar Skip to footer

How Do I Filter The Data Sent By Firestore

How do I filter the data in collection in Firestore If the first letter is '+' then I want to get filtered in Income section if the first letter is '-' then I want to get filtered

Solution 1:

You can loop through the list of values and append the values once rather than looping through it 5 times with a reduce & filter

  total_amount = db.collection("users").get().then((querySnapshot) => {
    var total_sum = 0;
    var income_sum = 0;
    var exp_sum = 0;
    
    querySnapshot.docs.forEach(doc => { 
          const amount = doc.data().amount;
          amount >= 0.0? income_sum += amount  :  exp_sum -= amount; 
          total_sum += amount;
    });

    return {total: total_sum, income: income_sum, expense: exp_sum }
})
.finally(result =>console.log("TOTALS", result);
  

EDIT It's not good to make major changes to your Question

but in general, you can truncate all number's to a decimal place using toFixed() on the final output

UPDATE

Ensure your numbers are converted to real numbers from user inputs with Number(value) if the typeof returned is not a number, you can know if it is valid or not.

Once this has been done, ensure you return the correct calls as needed as scripts also only fire once.

when rendering values inside HTML, you want to use an embedded <span> tag with the appropriate id tag rather than editing it via javascript

Post a Comment for "How Do I Filter The Data Sent By Firestore"