I am facing an issue with the function I'm using. It seems that when I attempt to do subtraction, both arrays are getting subtracted instead of just one.
Here are the variables:
let data1 = [
{ ProductTotalId: 30, ProductId: 30, Quantity: 50 },
{ ProductTotalId: 31, ProductId: 29, Quantity: 20 },
{ ProductTotalId: 32, ProductId: 28, Quantity: 30 },
{ ProductTotalId: 33, ProductId: 27, Quantity: 30 },
];
let Remove = [
{ ProductTotalId: 30, ProductId: 30, Quantity: 2 },
{ ProductTotalId: 30, ProductId: 30, Quantity: 10 },
{ ProductTotalId: 31, ProductId: 29, Quantity: 3 },
{ ProductTotalId: 32, ProductId: 28, Quantity: 12 },
{ ProductTotalId: 32, ProductId: 28, Quantity: 2 },
{ ProductTotalId: 33, ProductId: 27, Quantity: 11 },
{ ProductTotalId: 33, ProductId: 27, Quantity: 5 },
]
let data2
This is the function being used:
data2 = data1;
for (let value of data2) {
for (let data of Remove) {
if (value.ProductId === data.ProductId) {
value.Quantity = value.Quantity - data.Quantity;
}
}
}
console.log(data1);
console.log(data2);
The problem here is that 'data1' is equal to 'data2' with Quantity Subtracted. What I actually need is for 'data2' to be subtracted without any changes to 'data1'.
Link to Stackblitz: https://stackblitz.com/edit/typescript-u8mzii
Thank you for your attention.