0

I am trying to figure out how to manipulate data that's in an array in JS.

Currently I have this:

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']);
                    });
                });

I cant seem to figure out how to access the data within an array within the array.

I want to take the output:

[
  { _id: 567130, comdataPurchase: 525.49, defTotal: 38.79249 },
  { _id: 567132, comdataPurchase: 1050.98, defTotal: 77.58498 }
]

And subtract the defTotal from comdataPurchase.

Thank you so much in advance

1 Answer 1

1

Wow I am special lol

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(data){
                        console.log(data.comdataPurchase-data.defTotal);
                    });
                });

Solved

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.