(SOLVED)Getting sum of items obtained from a query

I have successfully found the results from a query and now i would like to find the sum of items that are in a field ‘referrals’ from the results of the same query. For instance, i get 7 items that matches the query parameters, how can i find the sum of items that are in the field ‘referrals’ from the 7 items? All suggestions will be appreciated. Here is my code:

 wixData.query("Referrals")
  .eq("referral", sumHrs)
  .eq("paid",500)
  .find()
  .then( (results1) => {
 let items = results1.items;
let item = items[0];
let referrals = item.referrals;

Use JS .reduce() method to calculate it.
See:

Thanks for the response. Apparently i have followed the steps on how to use a reducer and i don’t seem to understand. The reducer seems not to work. Kindly have a look at my code and advice according.

wixData.query("Referrals")
  .eq("referral", sumHrs)
  .eq("paid",500)
  .find()
  .then( (results1) => {
 let items = results1.items;
let referrals = items.referrals;
const reducer = (accumulator, currentValue) => accumulator + currentValue;
let indirect=referrals.reduce(reducer, 0)

@paiysurv

wixData.query("Referrals")
  .eq("referral", sumHrs)
  .eq("paid",500)
  .find()
  .then( (results1) => {
 let items = results1.items;
const  sumOfReferrals = items.reduce((a,c) => a + c.referrals, 0);
})

@jonatandor35 This has worked like magic!!! May your path be filled abundantly!!

Thanks, you’re welcome :slight_smile: