简体   繁体   中英

Manipulate data in an array within an array

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM