Having difficulty manipulating data stored in an array using JS.
Here's my current code snippet:
FuelReceipt.aggregate([
{$match:{date:{$gte: new Date(fuelStart), $lte: new Date(fuelEnd)}}},
{$group:{_id: '$truckNumber',
comdataPurchase:{$sum: '$comdataPurchase'},
defTotal:{$sum: '$defTotal'}}}]).exec(function(err, data){
if(err){
console.log('Error Fetching Model');
console.log(err);
}
console.log(JSON.stringify(data, null));
fuelArray = data;
console.log(fuelArray);
fuelArray.forEach(function(_id){
console.log(fuelArray['comdataPurchase']);
});
});
Struggling to access nested data within the array.
Goal is to manipulate the following output:
[
{ _id: 567130, comdataPurchase: 525.49, defTotal: 38.79249 },
{ _id: 567132, comdataPurchase: 1050.98, defTotal: 77.58498 }
]
Need to calculate the difference between comdataPurchase and defTotal for each entry.
Appreciate any help you can provide in advance.