I am currently working on my Angular 8 project and I am facing a challenge with merging two arrays into one while also increasing the quantity if they share the same value in the object. Despite several attempts, I have not been able to achieve the desired result.
mergedOrderList: any[]= [];
lstOldOrder: any[] = [
{id: "", products_id: "", qty: 1, ...},
{id: "", products_id: "", qty: 1, ...},
{id: "", products_id: "", qty: 1, ...},];
lstNewOrder: any[] = [
{id: "", products_id: "", qty: 1, ...},
{id: "", products_id: "", qty: 1, ...},
{id: "", products_id: "", qty: 1, ...},];
lstNewOrder.forEach(newOrder => {
let isDubplicated: boolean = false;
lstOldOrder.forEach(oldOrder => {
if (newOrder.products_id == oldOrder.products_id) {
oldOrder.qty += newOrder.qty;
isDubplicated = true;
mergedOrderList.push(oldOrder);
}
});
if (!isDubplicated) {
mergedOrderList.push(newOrder)
}
});
Although this method works when both orders have the same products_id, it encounters an issue when the new order lacks a products_id. In such cases, the old order gets skipped and only the new order is added to the list. I am uncertain whether I have implemented this correctly. Your assistance is greatly appreciated. Thank you.