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:
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
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 beNaN
).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):To summarize all the reduced values from multiple orders, you can use an additional
reduce
operation:The outer
reduce
iterates over each order in theorders
array. Inside the outerreduce
, the innerreduce
operation sums up theitemValue
of each item in theorderedItems
array for each order. The result is accumulated in theorderValue
variable.Finally, the outer
reduce
returns the accumulated total value (acc
) plus theorderValue
for each order. The initial value for the outerreduce
is set to0
, indicating that the total value starts at0
.