Forum Discussion

Madhu1's avatar
Madhu1
New Contributor
4 years ago
Solved

Improper result after Copy/Mapper output

Hi,

Can anyone pls help me why the values after copy snap getting changed without any transofrmation as shown in below pics.

In place of copy snap i used mapper also. Then also showing incorrect values for the Order quantity and Order value.

Below expressions used to calculate totalOrderQuantity and totalOrderValue.
$orderLineItems.reduce((acc,curr)=> acc+curr.totalItemQuantity,0)
$orderLineItems.reduce((acc,curr)=> acc+(curr.itemDenomination*curr.totalItemQuantity),0)

Pls let me know if you need the .slp
Thankyou.

  • Update - this issue was not related to the Array.reduce() function, but with how the target path was defined. After a short offline discussion with @Madhu1 we were able to resolve it using a Structure snap to push the results into the target object correctly.

3 Replies

  • Update - this issue was not related to the Array.reduce() function, but with how the target path was defined. After a short offline discussion with @Madhu1 we were able to resolve it using a Structure snap to push the results into the target object correctly.

  • Hi @Madhu1 - welcome to the Community!

    Based on what you have shown as the expressions used to calculated your values, you’re getting into an issue with memory addressing. The Array.reduce() function works on the array directly, so the concurrent executions of reduce() on the same array can yield unpredictable results (as you have seen). Try the following:

    [].concat($orderLineItems).reduce((acc,curr)=> acc+curr.totalItemQuantity,0)
    [].concat($orderLineItems).reduce((acc,curr)=> acc+(curr.itemDenomination*curr.totalItemQuantity),0)
    

    This should resolve the issue you are experiencing by first copying the array into a new memory address before performing the reduce() method.