【问题标题】:Manipulate data in an array within an array在数组中操作数组中的数据
【发布时间】:2020-05-02 19:29:17
【问题描述】:

我正在尝试弄清楚如何在 JS 中操作数组中的数据。

目前我有这个:

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

我似乎无法弄清楚如何访问数组中的数组中的数据。

我要输出:

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

然后从 comdataPurchase 中减去 defTotal。

非常感谢您

【问题讨论】:

    标签: javascript arrays mongodb mongoose


    【解决方案1】:

    哇,我很特别,哈哈

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

    解决了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 2018-05-09
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多