A
A
Artemiy_P2020-12-03 09:26:38
JavaScript
Artemiy_P, 2020-12-03 09:26:38

How to summarize orders by JSON data, JS?

{
   "result":[
      {
         "order_id":НННННН,
         "order_number":"НННННН",
         "posting_number":"НННННН",
         "status":"НННННН",
         "cancel_reason_id":НННННН,
         "created_at":"НННННН",
         "in_process_at":"НННННН",
         "products":[
            {
               "sku":ББББББ,
               "name":"ББББББ",
               "quantity":1,
               "offer_id":"ББББББ",
               "price":"1111.00"
            }
         ],
         "analytics_data":null,
         "financial_data":null
      },
      {
         "order_id":НННННН,
         "order_number":"НННННН",
         "posting_number":"НННННН",
         "status":"НННННН",
         "cancel_reason_id":НННННН,
         "created_at":"НННННН",
         "in_process_at":"НННННН",
         "products":[
            {
               "sku":ББББББ,
               "name":"ББББББ",
               "quantity":1,
               "offer_id":"ББББББ",
               "price":"1612.00"
            },
            {
               "sku":ВВВВВВ,
               "name":"ВВВВВВ",
               "quantity":1,
               "offer_id":"ВВВВВВ",
               "price":"1089.00"
            }
         ],
         "analytics_data":null,
         "financial_data":null
      },


It is necessary to sum up the sum of all orders, i.e. inside each order, calculate the amount of goods, and then add the orders themselves.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-12-03
@Artemiy_P

const calculateFinalAmount = cart => {
  return cart.reduce((cartTotal, record) => {
    const recordTotal = record.products.reduce((recordTotal, product) => {
      return recordTotal + parseFloat(product.price) * product.quantity;
    }, 0);
    
    return cartTotal + recordTotal;
  }, 0);
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question