(SOLVED) Loop through Dataset to multiply two fields

Hey all! I’ve been stuck trying to figure out how to loop through a dataset. My “Payroll” dataset has one field (column) containing the “worked hours”( hours field ) of my staff, and another field (column) containing a “value” for each of those “worked hours” ( value field ). What I’m trying to accomplish is to multiply the number of “worked hours” and the “value” fields for each of the items in my dataset. To write my code, I took as reference this other post, however, I haven’t had any luck so far. Please help!

let hoursTemp= 0 ;
let valueTemp= 0 ;
let toBePaidTemp= 0 ;

//first, let’s filter the dataset with the employee name and payroll period

await $w( “#datasetTimeSheets” ).setFilter(wixData.filter().eq( “payrollPeriod” , $w( ‘#dropdownPeriod’ ).value).contains( “employee” , $w( “#inputEmployee” ).value)).then(() =>
{
let count = $w( “#datasetTimeSheets” ).getTotalCount();
var results = $w( “#datasetTimeSheets” ).getItems( 0 , count);

//loop through each result to multiply hours and value
for ( let i= 0 ; i < count ; i++)
{
hoursTemp= results.items[i].hours;
valueTemp= results.items[i].value;
toBePaidTemp= toBePaidTemp + (hoursTemp*valueTemp)
}
$w( ‘#paycheckAmount’ ).value = toBePaidTemp;

})

Are you getting these inputs (hours and value) from form ?

If yes,
Create one more field called ‘amount’
And calculate it on frontend and save it to database

Try to work with DATA-COLLECTION instead of DATASET…

import wixData from 'wix-data';

var toBePaidTemp

$w.onReady(function () { myFunction() });

function myFunction () {
    wixData.query("myCollection") //<------- enter here the database-name
    .find()
    .then( (results) => {
 if(results.items.length > 0) {
 let firstItem = results.items[0];

 for (var i = 0; i < results.items.length; i++) {
 let hoursTemp= results.items[i].hours;
 let valueTemp= results.items[i].value;
            toBePaidTemp= toBePaidTemp + (hoursTemp*valueTemp)
        }
        $w('#paycheckAmount').value = toBePaidTemp;

        } else {
 // handle case where no matching items found
        }
    } )
    .catch( (err) => {
 let errorMsg = err;
    } );
}

I did not test it, but should work normaly.

Thank you, thank you!!! Your example helped me A LOT, and with a couple tweeks the following code works perfectly:

await wixData.query( “Payroll” )
.eq( " payrollPeriod " , $w( ‘#dropdownPeriod’ ).value)
.contains( " employee " , $w( ‘#inputEmployee’ ).value)
.limit( 1000 )
.find()
.then( (results) => {
if (results.items.length > 0 )
{
let count = results.items.length;
for ( var i = 0 ; i < count; i++)
{
currentItem = results.items[i];
toBePaidTemp = toBePaidTemp + (currentItem.hours *
currentItem.value);
}
$w( '# paycheckAmount ’ ).value = toBePaidTemp.toFixed( 2 );
}
else
{
$w( ‘#horasContables’ ).value = 0 ;
}
} )
. catch ( (err) => {
let errorMsg = err;
} );

You are welcome!