skip to Main Content

i download all my data into json.
I have data like

[
  {
    orderId: 1,
    orderedItems: [
      {
        itemId: 1,
        itemValue: 33.33
      },
      {
        itemId: 2,
        itemValue: 55.33
      }
    ]
  },
  {
    orderId: 2,
    orderedItems: [
      {
        itemId: 1,
        itemValue: 33.33
      },
      {
        itemId: 2,
        itemValue: 55.33
      }
    ]
  }
]

And with this data im doing the reduce, it look like:

item.productId
    .map(prod => {
      const product = item.magazyn.find(p => p.productId === prod.productId);
      if (product) {
        return (
          Math.round(
            (prod.productQuantity *
              (product.productNettoSellingPrice +
                product.productNettoSellingPrice * (product.productVatRate / 100)) -
              prod.productQuantity *
                (product.productNettoSellingPrice +
                  product.productNettoSellingPrice * (product.productVatRate / 100)) *
                (row.original.contractorIndividualDiscount / 100) -
              prod.productQuantity *
                (product.productNettoSellingPrice +
                  product.productNettoSellingPrice * (product.productVatRate / 100)) *
                (product.productDiscount / 100)) *
              100
          ) / 100
        );
      }
      return 0;
    })
    .reduce((total, currentValue) => total + currentValue, 0);

And it starting to look like this:
no description here

But the point is how to summary all data which was reduced before? I trying to make .reduce for this .reduce but gives me null or [Object object] 😛
This table is only the most easiest table in html, with table tr td…
Can someone help to summary all of those values?

I know how to summary all values of items of one order but i cannot summary all values of all orders.

2

Answers


  1. First thing, check if the .map call is returning an array of numbers (if any variable used inside is an object and not a number, then the result of the arithmetic expression will be NaN).

    If it is actually an array of numbers, then you can declare a variable and iterate over the returned array with .forEach, adding the price to the variable on each iteration (hardcoded reduction):

    var total = 0;
    item.productId
    .map(prod => {
      const product = item.magazyn.find(p => p.productId === prod.productId);
      if (product) {
        return (
          Math.round(
            (prod.productQuantity *
              (product.productNettoSellingPrice +
                product.productNettoSellingPrice * (product.productVatRate / 100)) -
              prod.productQuantity *
                (product.productNettoSellingPrice +
                  product.productNettoSellingPrice * (product.productVatRate / 100)) *
                (row.original.contractorIndividualDiscount / 100) -
              prod.productQuantity *
                (product.productNettoSellingPrice +
                  product.productNettoSellingPrice * (product.productVatRate / 100)) *
                (product.productDiscount / 100)) *
              100
          ) / 100
        );
      }
      return 0;
    })
    .forEach(val=>total+=val);
    
    Login or Signup to reply.
  2. To summarize all the reduced values from multiple orders, you can use an additional reduce operation:

    const orders = [
      {
        orderId: 1,
        orderedItems: [
          {
            itemId: 1,
            itemValue: 33.33
          },
          {
            itemId: 2,
            itemValue: 55.33
          }
        ]
      },
      {
        orderId: 2,
        orderedItems: [
          {
            itemId: 1,
            itemValue: 33.33
          },
          {
            itemId: 2,
            itemValue: 55.33
          }
        ]
      }
    ];
    
    const totalValue = orders.reduce((acc, order) => {
      const orderValue = order.orderedItems.reduce((orderAcc, item) => {
        return orderAcc + item.itemValue;
      }, 0);
      return acc + orderValue;
    }, 0);
    
    console.log("Total value:", totalValue); // Total value: 177.32
    

    The outer reduce iterates over each order in the orders array. Inside the outer reduce, the inner reduce operation sums up the itemValue of each item in the orderedItems array for each order. The result is accumulated in the orderValue variable.

    Finally, the outer reduce returns the accumulated total value (acc) plus the orderValue for each order. The initial value for the outer reduce is set to 0, indicating that the total value starts at 0.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search