【问题标题】:Deep Array of Objects sum using Lodash使用 Lodash 对对象进行求和的深度数组
【发布时间】:2021-11-26 20:49:10
【问题描述】:

我有一个目前看起来像这样的数据集:

let cart_items =  [
      {
        "id": "817fd815-302c-401a-b087-6781f073a2bf",
        "date": "2021-09-10",
        "notes": "null",
        "quantity": 2,
        "is_special": true,
        "mobile_price": "1.21",
        "item_name": "Cranberry",
        "menu_name": "Breakfast",
        "item_photo": "loading",
        "cart_add_ons": [
        ]
      }, 
      {
        "id": "817fd815-302c-401a-b087-6781f073a2bf",
        "date": "2021-09-11",
        "notes": "null",
        "quantity": 2,
        "is_special": true,
        "mobile_price": "7.21",
        "item_name": "Cranberry",
        "menu_name": "Breakfast",
        "item_photo": "loading",
        "cart_add_ons": [
          {
            "name": "Cake",
            "mobile_price": "2.3"
          },
          {
            "name": "Milk",
            "mobile_price": "12"
          }
        ]
      },
      {
        "id": "817fd815-302c-401a-b087-6781f073a2bf",
        "date": "2021-09-10",
        "notes": "null",
        "quantity": 2,
        "is_special": true,
        "mobile_price": "5.21",
        "item_name": "Raspberry",
        "menu_name": "Breakfast",
        "item_photo": "loading",
        "cart_add_ons": [
          {
            "name": "Cake",
            "mobile_price": "2.3"
          },
          {
            "name": "Milk",
            "mobile_price": "12"
          }
        ]
      }
    ];

如果 cart_add_ons 有任何数据,我实际上是在尝试将 cart_add_ons 中的所有附加价格相加。

我正在尝试使用以下方式来实现这一点,但我知道这是不正确的。是否接近正确的解决方案?

result = _.chain(cart_items)
      .groupBy('date')
      .map((value, key) => ({
        date: key,
        total_amount: _.sumBy(value, item => Number(item.mobile_price)),
        total_add_on: _.sumBy(value, function(o) { return o.cart_add_ons ? Number(o.cart_add_ons.mobile_price) : 0; }),
        item_data: value,
      }))
      .sortBy('date')
      .value();

顺便说一句,这是上述问题的结果:

[
   {
      "date": "2021-09-10",
      "total_amount": 6.42,
      "total_add_on": null,
      "item_data": [
         {
            "id": "817fd815-302c-401a-b087-6781f073a2bf",
            "date": "2021-09-10",
            "notes": "null",
            "quantity": 2,
            "is_special": true,
            "mobile_price": "1.21",
            "item_name": "Cranberry",
            "menu_name": "Breakfast",
            "item_photo": "loading",
            "cart_add_ons": []
         },
         {
            "id": "817fd815-302c-401a-b087-6781f073a2bf",
            "date": "2021-09-10",
            "notes": "null",
            "quantity": 2,
            "is_special": true,
            "mobile_price": "5.21",
            "item_name": "Raspberry",
            "menu_name": "Breakfast",
            "item_photo": "loading",
            "cart_add_ons": [
               {
                  "name": "Cake",
                  "mobile_price": "2.3"
               },
               {
                  "name": "Milk",
                  "mobile_price": "12"
               }
            ]
         }
      ]
   },
   {
      "date": "2021-09-11",
      "total_amount": 7.21,
      "total_add_on": null,
      "item_data": [
         {
            "id": "817fd815-302c-401a-b087-6781f073a2bf",
            "date": "2021-09-11",
            "notes": "null",
            "quantity": 2,
            "is_special": true,
            "mobile_price": "7.21",
            "item_name": "Cranberry",
            "menu_name": "Breakfast",
            "item_photo": "loading",
            "cart_add_ons": [
               {
                  "name": "Cake",
                  "mobile_price": "2.3"
               },
               {
                  "name": "Milk",
                  "mobile_price": "12"
               }
            ]
         }
      ]
   }
]

请让我知道我哪里出了问题。

【问题讨论】:

    标签: javascript node.js arrays object lodash


    【解决方案1】:

    没关系,我得到了答案。 但是,如果它对任何人有帮助,我将把它保留在这里。

      result = _.chain(cart_items)
          .groupBy('date')
          .map((value, key) => ({
            date: key,
            total_amount: _.sumBy(value, item => Number(item.mobile_price)),
            total_add_on: _.sumBy(value, function(item) { return item.cart_add_ons ? _.sumBy(item.cart_add_ons, k => Number(k.mobile_price)) : 0; }),
            item_data: value,
          }))
          .sortBy('date')
          .value();
    

    如果有的话,我们可以链 _.sumBy() 循环遍历内部对象数组 (cart_add_ons),然后对 mobile_prices 求和。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      相关资源
      最近更新 更多