How to store for loop results to an array?

Hi, I’m working on a code but unable to find a solution. Please help me. Actually, I need to find the sum of all the values for variable “interest”. Interest is calculated using the “rate” variable which comes from a for loop after checking the conditions.

if (wixUsers.currentUser.loggedIn) {
                wixData.query("transfirm").eq("_owner", wixUsers.currentUser.id).eq("name", $item('#rate').value)
                    .find()
                    .then((results) => {
                        let queryItems = results.items;
                            queryItems.forEach((item, idx) => {
                                for (idx = 0; idx <= queryItems.length; idx++) { 
                                     if ($item('#text18').text === item.name) {
                                        $item('#rate').text = item.rateofinterest;
                                         var rate = parseFloat($item('#rate').text);
                                    }
                                }
                                 var days = (parseFloat(($item('#days').text)));

                                 if (!isNaN(rate)) {
                                     var myrate = parseFloat(rate);
                                     var interest = parseFloat((principal * myrate * days) / 36000);
                                      console.log(interest);
                                      $item('#interest').text = interest.toString();                                                          
                            }
                        })

Now, I need total interest. I only put useful code because it’s little bit lengthy. Also, code is working perfectly. I’m considering: -

  • to store the interest variable in array one by one but failed to do so.
  • using reduce method; failed because array not formed.

I’ve tried a lot of methods but unable to get this. For a expert it may be just matter of few minutes. Looking forward to hearing from Velo community. Thanks in advance.

Something like this?

if(wixUsers.currentUser.loggedIn){                 wixData.query("transfirm").eq("_owner", wixUsers.currentUser.id).eq("name",$item('#rate').value).find().then((results)=>{let queryItems = results.items;                             queryItems.forEach((item, idx)=>{
    var totalInterest = 0;
    for(idx =0; idx <= queryItems.length; idx++)
        {
            if($item('#text18').text === item.name){
                $item('#rate').text = item.rateofinterest;
                var rate =parseFloat($item('#rate').text);
                if(rate){
                   var thisInterest =parseFloat((principal * myrate * days)/36000); 
                    totalInterest = totalInterest + thisInterest;
                                }
            }
        }
                console.log(totalInterest );
                $item('#interest').text = totalInterest.toString();}})

Did not run this, but it should be pretty close.

Hi, Thanks for the response. This also not work but I’ve made it working by initializing array at onready then pushing the each result into that array. :slight_smile:

Now, I’ve one more issue if you can take a look then it really be helpful. https://www.wix.com/velo/forum/community-discussion/need-help-with-repeater-array-dataset